I was able to solve it by simply digging into the Rails 4 documentation. Every instance of my Room
model has a method room_attribute_ids=
. Notice that Rails singularized room_attributes
to room_attribute
and appended an _ids
for the param, whereas my previous implementation used the pluralized one and the :name_of_associated_model_attributes => [:id]
convention.
Thus I list the room attributes in new.html.erb
as follows:
<label for="room_attributes">Attributes:</label>
<ul>
<% @room_attributes.each do |room_attribute| %>
<li>
<input type="checkbox" name="room[room_attribute_ids][]" value="<%= room_attribute.id %>">
<%= room_attribute.name %>
</li>
<% end %>
</ul>
Then in the controller I defined a private method to apply strong parameters for the nested attribute:
def room_params
params.require(:room).permit(:room_number, :room_type, :price, :room_attribute_ids => [])
end