I have a rails app as follows: A location model which stores some geo-stuff (a location basically), a post model and a user model. A post model can have a location. A user model can have a location as home location and another one as remote location:
class Location < ApplicationRecord
belongs_to :locationable, polymorphic: true
end
class Post < ApplicationRecord
has_one :location, as: :locationable
accepts_nested_attributes_for :location
end
class User < ApplicationRecord
has_one :homelocation, as: :locationable, class_name: 'Location'
has_one :remotelocation, as: :locationable, class_name: 'Location'
accepts_nested_attributes_for :homelocation, :remotelocation
end
The post and location stuff works great. If I delete one of the ´has_one´ lines from the user model and rename homelocation to location, everything works great too. If I want a user to have two different locations though, I get an 'Unpermitted parameters: homelocation, remotelocation' error when trying to save changes.
My users_controller has a
def user_params
params.require(:user).permit(:admin, :name, :motto, homelocation_attributes: [:id, :address], remotelocation_attributes: [:id, :address])
end
just as the posts_controller has a
def post_params
params.require(:post).permit(:title, :content, location_attributes: [:id, :address])
end
My forms look like this:
.form-group.string.required.user_homelocation_address
label.control-label.string.required for="user_homelocation_address"
abbr title="required"
| Home Location
input#user_homelocation_address.form-control.string.required name="user[homelocation][address]" type="text"
.form-group.string.required.user_remotelocation_address
label.control-label.string.required for="user_remotelocation_address"
abbr title="required"
| Remote Location
input#user_remotelocation_address.form-control.string.required name="user[remotelocation][address]" type="text"
So why does this work for one 'has_one', but not for two?