Validation in ruby on rails

60 views
Skip to first unread message

manoj c.

unread,
Jul 20, 2012, 6:47:20 AM7/20/12
to rubyonra...@googlegroups.com
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 ??

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

Rafi A

unread,
Jul 20, 2012, 6:53:26 AM7/20/12
to rubyonra...@googlegroups.com
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 | rafi...@gmail.com 
In Every moment, thank God.



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



manoj c.

unread,
Jul 23, 2012, 12:28:59 AM7/23/12
to rubyonra...@googlegroups.com
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 | rafi...@gmail.com
> <srk...@apigee.com>*In Every moment, thank God.*

Thanks a lot sir.
regards,
manoj

Ace Suares

unread,
Jul 23, 2012, 7:29:43 AM7/23/12
to rubyonra...@googlegroups.com

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

manoj c.

unread,
Jul 24, 2012, 6:42:53 AM7/24/12
to rubyonra...@googlegroups.com
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

Ace Suares

unread,
Jul 24, 2012, 8:35:45 AM7/24/12
to rubyonra...@googlegroups.com

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

Manoj M.

unread,
Jul 25, 2012, 12:30:14 AM7/25/12
to rubyonra...@googlegroups.com
Ace S. wrote in post #1069974:
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

Ace Suares

unread,
Jul 25, 2012, 9:19:47 AM7/25/12
to rubyonra...@googlegroups.com
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

<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

Manoj M.

unread,
Jul 26, 2012, 1:42:25 AM7/26/12
to rubyonra...@googlegroups.com
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,

vishal singh

unread,
Jul 26, 2012, 1:53:54 AM7/26/12
to rubyonra...@googlegroups.com
try to write this 

<% if @variable_name.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@ variable_name .errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @ variable_name .errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


Regards,
Vishal Singh






--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages