Note that there are some explanatory texts on larger screens.

plurals
  1. PORails checkboxes for nested attributes return an array, not hash -- getting HashWIthIndifferentAccessError
    text
    copied!<p>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.</p> <p>Following the solution <a href="https://stackoverflow.com/questions/727300/nested-object-w-checkboxes-mass-assignment-even-with-accepts-nested-attribute?rq=1">here</a> almost exactly, I get the following parameters:</p> <pre><code>--- !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 </code></pre> <p>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.</p> <pre><code> {"user"=&gt;{"password_confirmation"=&gt;"[FILTERED]", "roles_attributes"=&gt;{"id"=&gt;"2"}, ... </code></pre> <p>In <a href="https://stackoverflow.com/questions/3765810/why-is-accepts-nested-attributes-for-not-working-for-me-rails-3">this question</a>, 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.</p> <p>Here is the view:</p> <pre><code>&lt;% provide(:title, 'Create site') %&gt; &lt;h1&gt;Create meeting&lt;/h1&gt; &lt;div class="row"&gt; &lt;div class="span6 offset3"&gt; &lt;%= form_for(@meeting) do |f| %&gt; &lt;%= render :partial =&gt; 'shared/error_messages', :locals =&gt; {:object =&gt; @meeting} %&gt; &lt;%= f.label :date %&gt; &lt;%= f.text_field :date %&gt; &lt;% @rooms.each do |room| %&gt; &lt;%= f.radio_button :room_id, room.id %&gt; &lt;%= room.name %&gt;&lt;br /&gt; &lt;% end %&gt; &lt;%= f.fields_for :committee_meetings do |builder| %&gt; &lt;% committee = Committee.find(builder.object.committee_id) %&gt; &lt;li&gt;&lt;%= builder.check_box :committee_id, { :checked =&gt; false }, builder.object.committee_id %&gt; &lt;%= builder.label :committee_id, "#{committee.name}" %&gt; &lt;/li&gt; &lt;% end %&gt; &lt;%= f.submit "Create meeting", class: "btn btn-large btn-primary" %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Here is the Meetings controller:</p> <pre><code>class MeetingsController &lt; 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 </code></pre> <p>Here is the model:</p> <pre><code>class Meeting &lt; 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 =&gt; true has_many :committees, through: :committee_meetings belongs_to :room belongs_to :creator, :class_name =&gt; 'User' belongs_to :updater, :class_name =&gt; 'User' end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload