2

i'm trying to create a multiple nested form I have been following this example http://guides.rubyonrails.org/form_helpers.html#building-complex-forms

here we have

  • Person > has_many :addresses
  • Address > belong_to :person

But if I want to add the City table I would have

  • Person > has_many :addresses
  • Address > belongs_to :person, belongs_to :city
  • City > has_many :addresses

The problem comes when I try to add in the form the field City as a text_field NOT as a select. All of the examples I've seen use select instead.

What I want to do is to have a form to create a Person, allow the Person to insert Address and display City as a text field. If the city doesn't exist in the database then create it, if not use the existing one (I guess here I will have to use find_or_create_by?).

Person

class Person < ActiveRecord::Base
  has_many :addresses
  accepts_nested_attributes_for :addresses
end

Address

class Address < ActiveRecord::Base
  belongs_to :person
  belongs_to :city
  accepts_nested_attributes_for :city
end

City

class City < ActiveRecord::Base
  has_many :addresses
end

I dont know how to handle the person params, I have this:

def person_params
    params.require(:person).permit(:name, addresses_attributes: [:id, :kind, :street, cities_attributes: [:id, :city]])
  end

View

<%= form_for @person do |f| %>
  Addresses:
  <ul>
    <%= f.fields_for :addresses do |addresses_form| %>
      <li>
        <%= addresses_form.label :kind %>
        <%= addresses_form.text_field :kind %>

        <%= addresses_form.label :street %>
        <%= addresses_form.text_field :street %>

        <%= addresses_form.fields_for :cities do |cities_form| %>
           <%= cities_form.label :city %>
           <%= cities_form.text_field :city %>
        <% end %>
      </li>
    <% end %>
  </ul>
<% end %>

When i try to add the city i get this error

Unpermitted parameters: cities

And it doesn't add the city to the database nor the Address.

I have been trying to solve this for a while and I haven't able to find a solution. Any idea of what I'm doing wrong?

2
  • Show us your Person model. Commented Jun 5, 2014 at 5:41
  • I modified the original post to show the 3 models Commented Jun 5, 2014 at 12:48

1 Answer 1

1

That should be city_attributes. An address doesn't have_many :cities, just one. :)

Likewise, you must change the Address so that it belongs_to :city and accepts_nested_attributes_for :city

Sign up to request clarification or add additional context in comments.

3 Comments

Thats how I have it now
Your code above does not have it that way. it has accepts_nested_attributes_for :cities note the pluralisation. it should instead be accepts_nested_attributes_for :city You also have cities_attributes: [:id, :city] and that should be: city_attributes: [:id, :city]
I finally understood what you were traying to say, and it worked! Thanks genius

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.