Saturday, July 13, 2013

[Rails] Re: Nested attributes - not working in forms

Thank you, especially for linking the Railcast!

Watching this railcast helped me solving tis problem. However, .build is just an alias for .new, which I have already in def new.

The problem was about this line in the view:

<%= form_for :person, url: people_path do |f| %>

It should be


<%= form_for @person, url: people_path do |f| %>
Am Samstag, 13. Juli 2013 13:35:34 UTC+2 schrieb javinto:

instead.

Something else: a dedicated form object would be a better practise, right? Is there a detailled instruction on how to make a form object? Or a tutorial on adding classes not connected to ActiveRecords in general?

Thank you!


The fields_for form helper can only loop over association phone numbers that exist. The way to have new phone numbers added is to build them in advance. E.g.

@person.phones.build

Or if you want to supply 4 phone number fields:

4.times {@person.phone.build}

In that case you should add a reject_if option to your accept_nested_attributes :phone statement to have empty left phone numbers skipped.

You can build the phone numbers in you controller or use a form_helper like setup_person(person) in your forms.

See this Rails Cast: http://railscasts.com/episodes/196-nested-model-form-part-1

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ad53f10e-7b81-4f3c-9546-cdd5c6e96091%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate