2

Usually I just use something similar to http://railscasts.com/episodes/196-nested-model-form-part-1

Now i have an existing User which i am trying to add new cars and annimals. The User has_many cars and annimals and accepts_nested_attributes_for cars and annimals

If i do

= form_for(@user) do |f|
  = f.fields_for @user.cars.build do |c|

I get error:

unknown attribute: car

And if i do

= form_for(@user) do |f|
  = f.fields_for :cars do |c|

I get a list of all existing cars for that user (when I want to make a new one)

Thannks!

ps: i guess i should add, i'm using simple_form_for and simple_fields_for, it might be a bug with that...

1
  • Do you have accepts_nested_attributes_for in the User model? Commented Mar 9, 2011 at 16:26

1 Answer 1

4

Deep inside the rails api I found:

It’s also possible to specify the instance to be used:

  <%= form_for @person do |person_form| %>
    ...
    <% @person.projects.each do |project| %>
      <% if project.active? %>
        <%= person_form.fields_for :projects, project do |project_fields| %>
          Name: <%= project_fields.text_field :name %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>

So in my case, i guess the syntax would be

= form_for(@user) do |f|
  = f.fields_for :cars, @user.cars.build do |c|
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.