How do you use :message with validation?

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 L.

Ben L. 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 L.

Ben,

You need to use flash messages in your views:
http://rubyonrails.org/api/classes/ActionController/Flash.html

  • Jean-Etienne

Carl-Johan K. 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.

Check the API for ActiveRecord::Errors and error_messages_for.

Jean-etienne Durand wrote:

Ben L. 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 L.

Ben,

You need to use flash messages in your views:
http://rubyonrails.org/api/classes/ActionController/Flash.html

  • Jean-Etienne

That’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?

<%= 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 }

On 7/20/06, Ben L. [email protected] wrote:

159 display: table;
170 font-weight: bold;
181 #errorExplanation ul li {
for i in ([email protected])
flash[:notice] += “
” + @messages[i]
end


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

You 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("
")

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

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 ([email protected])
    flash[:notice] += "<br>" + @messages[i]
  end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs