Validation in ruby on rails

Hi,
   Please Help me,in a model i gave like validates_presence_of :name,now
i
need to display error message like "name is required" while clicking
form submission button when leaving text box for name as empty,how
to do this ??

Manoj,

try this in the model:

validates_presence_of :name, :message => “can’t be blank”

Regards,
Seeni Rafiyullah Khan A,
Skype id: rafiyullah.railsfactory | +91 9786832092 | rafiglitz@gmail.com
In Every moment, thank God.

Rafi A wrote in post #1069463:

Manoj,

try this in the model:

validates_presence_of :name, :message => "can't be blank"

Regards,
Seeni Rafiyullah Khan A,
Skype id: rafiyullah.railsfactory | +91 9786832092 | rafiglitz@gmail.com
<srkhan@apigee.com>*In Every moment, thank God.*

Thanks a lot sir.
regards,
manoj

Dear sirs,

I dont’t think the addition of :message => “can’t be blank” does make a difference.\

Validations have their own strings already packed, in en.yml in the active_model gem. In the console (rails c) you can try it out (assuming your model is named ‘Person’):

p=Person.new
p.valid?
p.errors

or p.errors[:name]

You will see that it will show a message “can’t be blank” also when you don’t’ specify :message in the validation!

So, these messages are already generated, now you want to show them, look here: http://guides.rubyonrails.org/active_record_validations_callbacks.html#displaying-validation-errors-in-the-view for documentation.

Also, have a look at validation_hints gem (https://github.com/acesuares/validation_hints) which helps you display helpful context sensitive messages for form fields</shameless plug>!

Cheers,
ace

Ace S. wrote in post #1069767:

Dear sirs,

I dont't think the addition of :message => "can't be blank" does make a
difference.\

Validations have their own strings already packed, in en.yml in the
active_model gem. In the console (rails c) you can try it out (assuming
your model is named 'Person'):

p=Person.new
p.valid?
p.errors
# or p.errors[:name]

You will see that it will show a message "can't be blank" also when you
don't' specify :message in the validation!

So, these messages are already generated, now you want to show them,
look
here:

http://guides.rubyonrails.org/active_record_validations_callbacks.html#displaying-validation-errors-in-the-view

for documentation.

Also, <shameless plug>have a look at validation_hints gem
(https://github.com/acesuares/validation_hints) which helps you display
helpful context sensitive messages for form fields</shameless plug>!

Cheers,
ace

Dear ace,
          as per you have told, i wrote <%= f.error_messges %> in my
view page to display error when leaving name as empty , but right now am
getting like "undefined error_messages", please help me to solve this
problem .

regards,
manoj

Hi manoj,

I am replying to a message that you posted on the ruby-forum but didn’t show up in google groups.

Anyway, did you put

gem ``"dynamic_form"

in your Gemfile?
And then after that, run ‘bundle install’ ?
Please do so and try again.

cheers
ace

Ace S. wrote in post #1069974:

Hi manoj,

I am replying to a message that you posted on the ruby-forum but didn't
show up in google groups.

Anyway, did you put

gem "dynamic_form"

in your Gemfile?
And then after that, run 'bundle install' ?
Please do so and try again.

cheers
ace

Hi ace,
        yes i did like that you told, included gem "dynamic_form" in
gemfile and bundle install is done, but now also its not working. What
to do next? help me.

Regards,
manoj

Manoj,

Open up the console (rails c), and then create a new User (or whatever model you want) like this:

u = User.new

Check if this user is valid:

u.valid?

This should result in false! if NOT, then you don’t’ have any errors. Then you have to look at you validation lines in the model…

If it is false then try:

u.errors
u.errors.full_messages

this SHOULD give you an array of error messages. If not, there’s something wrong.

Now go back to your view and insert

ERRORS: <%= @object.errors.full_messages %> !!! %>

into your view. Of course the @object must match the one you are making the form for.

Tell me what you see then.

Cheers
ace

Ace S. wrote in post #1070147:

Manoj,

Open up the console (rails c), and then create a new User (or whatever
model you want) like this:

u = User.new

Check if this user is valid:

u.valid?

This should result in false! if NOT, then you dont' have any errors.
Then
you have to look at you validation lines in the model...

If it is false then try:

u.errors
u.errors.full_messages

this SHOULD give you an array of error messages. If not, there's
something
wrong.

Now go back to your view and insert

<p>ERRORS: <%= @object.errors.full_messages %> !!! %></p>

into your view. Of course the @object must match the one you are making
the
form for.

Tell me what you see then.

Cheers
ace

Dear ace,
          i tried with console it just works fine that showing
error_message ["Name can't be blank"] when i gave a.errors.full_messages
where a is a new object, but am unable to print the error message in
view page,where only empty array is displayed, i have no idea what to
do, its been more than 4 days i have stucked please help me out,
expecting your help more.

regards,
manoj

try to write this

<% if @variable_name.errors.any? %>

<%= pluralize(@ variable_name .errors.count, "error") %> prohibited this user from being saved:

    <% @
    variable_name .errors.full_messages.each do |msg| %>

  • <%= msg %>
  • <% end %>

<% end %>

Regards,

Vishal Singh