Forum: Ruby on Rails How to display error in view for an instance

Posted by renu mehta (sm94582)
on 2012-07-03 07:59
Hello,

How can we display the errors in a view if that view contains one
instance of type member and an array also of instances from the same
model. In my app I have a form which accepts a member data for
registration for a family on the top and at the end of the screen in the
same form I accept the data for the rest of the family members which
also belong to the same model. After the validation I get the errors for
'@member' but not the '@familt_members'.
@member is created as

@member=Member.new

and @member_list is created as :

@member_list=[]
5.times{
@member_list<<Member.new
    }

After the validations how do I access the errors related to each
instance in the array?

thanks.
Posted by Walter Davis (walterdavis)
on 2012-07-03 17:05
(Received via mailing list)
On Jul 3, 2012, at 1:59 AM, renu mehta wrote:

>
> instance in the array?
What do you see if you iterate over the members of the array, like this 
(in a view, just for debugging purposes)?

  <%- @member_list.each do |member| %>
  <%= member.inspect %>
  <%- end %>

I am guessing that each member will carry its own hash of errors, and 
you should see them when you do this. Then it's just a matter of 
iterating them in your view to build the error list per member.

Walter
Posted by renu mehta (sm94582)
on 2012-07-03 19:05
Walter Davis wrote in post #1067201:
> On Jul 3, 2012, at 1:59 AM, renu mehta wrote:
>
>>
>> instance in the array?
> What do you see if you iterate over the members of the array, like this
> (in a view, just for debugging purposes)?
>
>   <%- @member_list.each do |member| %>
>   <%= member.inspect %>
>   <%- end %>
>
> I am guessing that each member will carry its own hash of errors, and
> you should see them when you do this. Then it's just a matter of
> iterating them in your view to build the error list per member.
>
> Walter

This is what I get:

#<Member family_member_id: nil, member_first_name: "", member_last_name: 
"", security_question_answer: nil, created_at: nil, updated_at: nil, 
member_gender: nil, member_dob: nil, security_question_id: nil, 
native_language: "", family_id: nil, is_child: false, is_family_head: 
false, member_middle_name: "", member_email: nil, relation_name: "", 
marital_status: nil>

But nothing about errors. And following code in the loop does not show 
anything.

<% member.errors.full_messages.each do |msg| %>

      <span style="color:red;"><%= msg %></span>
    <% end %>
Posted by Walter Davis (walterdavis)
on 2012-07-03 21:18
(Received via mailing list)
On Jul 3, 2012, at 1:05 PM, renu mehta wrote:

>>  <%- end %>
> "", security_question_answer: nil, created_at: nil, updated_at: nil,
>      <span style="color:red;"><%= msg %></span>
>    <% end %>
>

At this point in the cycle, have you already tried to save these 
members? Or are they being saved through an association? I don't think 
the errors are populated on any AR object until you actually try to 
save, because those validations only run within the save cycle.

Walter
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.