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?
Personmodel.