I have Committee and Meeting objects. When creating a Meeting, I want to associate a Committee or Committees at the same time. CommitteeMeeting is a nested attribute of Meeting.
Following the solution here almost exactly, I get the following parameters:
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: us1hzugov7DDIpKNobOZJbuk14KsIsoz3uJRZEy2VRc=
meeting: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
date: '2001-01-03'
room_id: '1'
committee_meetings_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
'0': !ruby/hash:ActiveSupport::HashWithIndifferentAccess
committee_id: '1'
'1': !ruby/hash:ActiveSupport::HashWithIndifferentAccess
committee_id: '2'
commit: Create meeting
action: create
controller: meetings
The committee_meeting_attributes are properly nested, but in an array, which seems to be what is causing the error. Rails is expecting a hash. The solution I'm using as a reference gets a hash in the parameters.
{"user"=>{"password_confirmation"=>"[FILTERED]",
"roles_attributes"=>{"id"=>"2"}, ...
In this question, the developer got a similar error in his parameters, but his problem was not having the nested attributes accessible in the parent model. I do have the nested attributes accessible in the model.
Here is the view:
<% provide(:title, 'Create site') %>
<h1>Create meeting</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@meeting) do |f| %>
<%= render :partial => 'shared/error_messages',
:locals => {:object => @meeting} %>
<%= f.label :date %>
<%= f.text_field :date %>
<% @rooms.each do |room| %>
<%= f.radio_button :room_id, room.id %> <%= room.name %><br />
<% end %>
<%= f.fields_for :committee_meetings do |builder| %>
<% committee = Committee.find(builder.object.committee_id) %>
<li><%= builder.check_box :committee_id, { :checked => false },
builder.object.committee_id %>
<%= builder.label :committee_id, "#{committee.name}" %>
</li>
<% end %>
<%= f.submit "Create meeting", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
Here is the Meetings controller:
class MeetingsController < ApplicationController
def create
@rooms = Room.all
@committees = Committee.all
@meeting = Meeting.new(params[:meeting])
@meeting.creator_id = current_user.id
@meeting.updater_id = current_user.id
if @meeting.save
flash[:success] = "You have succesfully created a meeting on #{@meeting.date}
in #{@meeting.room.site.name}, #{@meeting.room.name}!"
redirect_to root_url
else
render 'new'
end
end
def new
@rooms = Room.all
@meeting = Meeting.new
@committees = Committee.all
end
Here is the model:
class Meeting < ActiveRecord::Base
attr_accessible :creator_id, :date, :room_id, :updater_id, :committee_meetings_attributes
has_many :committee_meetings
accepts_nested_attributes_for :committee_meetings, :allow_destroy => true
has_many :committees, through: :committee_meetings
belongs_to :room
belongs_to :creator, :class_name => 'User'
belongs_to :updater, :class_name => 'User'
end