If I do validates_uniqueness_of :name, :message => "It''s not uniqueeee!!!" then how do I have it display that message when the check fails? -Ben Lisbakken -- Posted via http://www.ruby-forum.com/.
Check the API for ActiveRecord::Errors and error_messages_for. On 7/20/06, Ben Lisbakken <lisbakke@gmail.com> wrote:> If I do validates_uniqueness_of :name, :message => "It''s not > uniqueeee!!!" > > then how do I have it display that message when the check fails? > > -Ben Lisbakken > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jean-etienne Durand
2006-Jul-20 09:34 UTC
[Rails] Re: How do you use :message with validation?
Ben Lisbakken wrote:> If I do validates_uniqueness_of :name, :message => "It''s not > uniqueeee!!!" > > then how do I have it display that message when the check fails? > > -Ben LisbakkenBen, You need to use flash messages in your views: http://rubyonrails.org/api/classes/ActionController/Flash.html - Jean-Etienne -- Posted via http://www.ruby-forum.com/.
Carl-Johan Kihlbom wrote:> Check the API for ActiveRecord::Errors and error_messages_for.I am... all i can find is @user.errors.full_messages.... but I want to display each error with formatting. -- Posted via http://www.ruby-forum.com/.
Jean-etienne Durand wrote:> Ben Lisbakken wrote: >> If I do validates_uniqueness_of :name, :message => "It''s not >> uniqueeee!!!" >> >> then how do I have it display that message when the check fails? >> >> -Ben Lisbakken > > Ben, > > You need to use flash messages in your views: > http://rubyonrails.org/api/classes/ActionController/Flash.html > > - Jean-EtienneThat''s a part of it, yes. That''s how I''ll display the message... but I''m referring to the parameter :message in a validates_uniqueness_of line. How do I access that after the test fails? -- Posted via http://www.ruby-forum.com/.
Jean-etienne Durand
2006-Jul-20 09:43 UTC
[Rails] Re: How do you use :message with validation?
<%= error_messages_for(''my_model'') %> in your view. Some css style examples: 154 /* error 155 ---- */ 156 .fieldWithErrors { 157 padding: 1px; 158 background-color: red; 159 display: table; 160 } 161 #errorExplanation { 162 border: 2px solid red; 163 padding: 7px; 164 padding-bottom: 12px; 165 margin-bottom: 10px; 166 background-color: #f0f0f0; 167 } 168 #errorExplanation h2 { 169 text-align: left; 170 font-weight: bold; 171 padding: 5px 5px 5px 15px; 172 margin: -7px; 173 background-color: #c00; 174 color: #fff; 175 } 176 #errorExplanation p { 177 color: #333; 178 margin-bottom: 0; 179 padding: 5px; 180 } 181 #errorExplanation ul li { 182 list-style: square; 183 } -- Posted via http://www.ruby-forum.com/.
Jean-etienne Durand wrote:> <%= error_messages_for(''my_model'') %> in your view. > > Some css style examples: > 154 /* error > 155 ---- */ > 156 .fieldWithErrors { > 157 padding: 1px; > 158 background-color: red; > 159 display: table; > 160 } > 161 #errorExplanation { > 162 border: 2px solid red; > 163 padding: 7px; > 164 padding-bottom: 12px; > 165 margin-bottom: 10px; > 166 background-color: #f0f0f0; > 167 } > 168 #errorExplanation h2 { > 169 text-align: left; > 170 font-weight: bold; > 171 padding: 5px 5px 5px 15px; > 172 margin: -7px; > 173 background-color: #c00; > 174 color: #fff; > 175 } > 176 #errorExplanation p { > 177 color: #333; > 178 margin-bottom: 0; > 179 padding: 5px; > 180 } > 181 #errorExplanation ul li { > 182 list-style: square; > 183 }thanks - I''m afraid I wasn''t specific enough. Wanted the error message in my controller, not the view. here''s how I did it @messages = @user.errors.full_messages flash[:notice] = @messages[0] for i in (1..@messages.length-1) flash[:notice] += "<br>" + @messages[i] end -- Posted via http://www.ruby-forum.com/.
On 7/20/06, Ben Lisbakken <lisbakke@gmail.com> wrote:> > Jean-etienne Durand wrote: > > <%= error_messages_for(''my_model'') %> in your view. > > > > Some css style examples: > > 154 /* error > > 155 ---- */ > > 156 .fieldWithErrors { > > 157 padding: 1px; > > 158 background-color: red; > > 159 display: table; > > 160 } > > 161 #errorExplanation { > > 162 border: 2px solid red; > > 163 padding: 7px; > > 164 padding-bottom: 12px; > > 165 margin-bottom: 10px; > > 166 background-color: #f0f0f0; > > 167 } > > 168 #errorExplanation h2 { > > 169 text-align: left; > > 170 font-weight: bold; > > 171 padding: 5px 5px 5px 15px; > > 172 margin: -7px; > > 173 background-color: #c00; > > 174 color: #fff; > > 175 } > > 176 #errorExplanation p { > > 177 color: #333; > > 178 margin-bottom: 0; > > 179 padding: 5px; > > 180 } > > 181 #errorExplanation ul li { > > 182 list-style: square; > > 183 } > > thanks - I''m afraid I wasn''t specific enough. Wanted the error message > in my controller, not the view. > > here''s how I did it > > @messages = @user.errors.full_messages > flash[:notice] = @messages[0] > for i in (1..@messages.length-1) > flash[:notice] += "<br>" + @messages[i] > end > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsYou can use the errors.on(:field) if you want to get the message for a field. Or even just errors[:field] As for you example code is there a reason why you couldn''t just use something like flash[:notice] = @user.errors.full_messages.join("<br/>") The error_messages_for will provide you with a display in your view of all these messages, and there is a post from Jean-etienne Durand with an example style. If you don''t want to display some of the portions of what is returned from error_messages_for why not control it with CSS. eg errorExplaination h2 { display:none; } errorExplaination p { display:none; } Just my 2c -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060720/15e62662/attachment.html