嵌套属性的Rails复选框返回一个数组,而不是哈希 - 获取HashWIthIndifferentAccessError

时间:2013-01-23 11:02:08

标签: ruby-on-rails-3 nested-attributes

我有委员会和会议对象。在创建会议时,我想同时关联一个或多个委员会。 CommitteeMeeting是Meeting的嵌套属性。

几乎完全按照解决方案here,我得到以下参数:

--- !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

委员会_meeting_attributes已正确嵌套,但在数组中,这似乎是导致错误的原因。 Rails期待一个哈希。我用作参考的解决方案在参数中得到一个哈希值。

    {"user"=>{"password_confirmation"=>"[FILTERED]", 
"roles_attributes"=>{"id"=>"2"}, ...

this question中,开发人员的参数出现了类似的错误,但他的问题是没有在父模型中访问嵌套属性。我确实可以在模型中访问嵌套属性。

以下是观点:

<% 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>

以下是会议控制员:

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 

以下是模型:

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

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,我可以告诉你我做了什么,但它可能太聪明了一半,或者可能有一种更直接的方法来实现它,我只是不知道。

对我来说,我希望我的所有委员会每次都以相同的顺序显示,所以我首先在我的newedit控制器中预建所有可能的关联以及{的失败情况{1}}和create

我这里有代码,可以确保所有可能存在的关联,尊重保存的关联,并且每次都以相同的方式对它们进行排序:

Rails: How do I prepend or insert an association with build?

然后在我看来,我使用复选框删除我不想保存的复选框。

update

基本上,我已经颠倒了复选框的极性,因此如果没有选中则会发送= f.fields_for :committee_meetings do |cm| = cm.check_box :_destroy, {:checked => cm.object.persisted?}, 0, 1 = cm.label :_destroy, cm.object.committee.name = cm.hidden_field :committee_id, :value => f.object.committee.id ,如果没有则会发送。{/ p>

它对我有用,我相信你可以让它为你工作。我担心我不能说为什么你的代码失败了,因为我不明白为什么它甚至部分工作。我更愿意解决你的问题,但也许这种替代方法会有所帮助。