Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 分组的\u集合\u选择的有\u多个:通过关联未更新_Ruby On Rails_Ruby_Ruby On Rails 3_Activerecord_Associations - Fatal编程技术网

Ruby on rails 分组的\u集合\u选择的有\u多个:通过关联未更新

Ruby on rails 分组的\u集合\u选择的有\u多个:通过关联未更新,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,associations,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,Associations,我有一个场景,我创建了一些have\u many:通过我在更新时遇到困难的关联。我非常感谢你的帮助 以下是我的设置: ruby 1.9.3p392 轨道3.2.13 以下是场景: 我有3个相关的模型 有很多行业,很多关键词,还有很多会议 行业有很多关键词 会议有一个行业 会议根据行业选择关键词 关键字有一个基本的CRUD模型。创建一个行业,然后添加关键字,效果很好 我与会议、行业和关键词的关系是我遇到问题的地方。我正在尝试更新会议信息,更改当前与之关联的行业,根据新行业选择新的关键字,所

我有一个场景,我创建了一些have\u many:通过我在更新时遇到困难的关联。我非常感谢你的帮助

以下是我的设置:

  • ruby 1.9.3p392
  • 轨道3.2.13
以下是场景:

  • 我有3个相关的模型
  • 有很多行业,很多关键词,还有很多会议
  • 行业有很多关键词
  • 会议有一个行业
  • 会议根据行业选择关键词
关键字有一个基本的CRUD模型。创建一个行业,然后添加关键字,效果很好

我与会议、行业和关键词的关系是我遇到问题的地方。我正在尝试更新会议信息,更改当前与之关联的行业,根据新行业选择新的关键字,所有这些都是相同的形式。当我从混合中删除Keywords元素时,更新工作正常。但当这三个都存在时,我就得到了错误

我的行业关系和关键词是:

class IKeywordable < ActiveRecord::Base

attr_accessible :industry_id, :keyword_id

belongs_to :industry
belongs_to :keyword

end

视图呈现良好,但当我提交更改时,会出现错误。我看到的错误是:

对于ID=1的会议,找不到ID=3的行业

以下是传递的参数:


{“utf8”=>“✓", "_方法“=>”投入“,”真实性令牌“=>”k9fcAXF/IOXT3OVUTCXEDCAWOFE2PRVLBFBQG8MO/I=“,”会议“=>{”名称“=>”测试会议“,”地址1“=>”地址2“,”城市“=>”城市“,”州id“=>”38“,”电子邮件“=>”test@example.com“,“web”=>“www.example.com”,“行业id”=>“1”,“行业属性”=>{“关键字”=>[”,“6”,“5”],“id”=>“3”}},“commit”=>“Update Conference”,“action”=>“Update”,“controller”=>“conferences”,“id”=>“1”}

我认为问题在于会议类中的以下两个语句之间存在冲突:

class Conference
  ...

  has_many :keywords, through: :c_keywordables

  ...

  serialize :keywords, Array do
    ...
  end
end

对关键字的第一个引用期望与关键字表建立ActiveRecord关联,而第二个引用期望有一个名为keywords的db备份属性(列),该属性将保存数组数据的序列化(yaml)表示。他们在为如何帮助你而斗争,而你是战斗中的失败者。

@AndyV,我终于解决了这个问题。结果是我给协会打电话的方式出了问题。我进入控制台,进一步调查我是如何与关联进行交互的。我想通过行业将关键词与会议联系起来,这是错误的。因此,我:

 has_many :c_keywordables
 has_many :keywords, through: :c_keywordables
我可以直接调用关键字。因此,在控制台中,我可以执行以下操作:

 > c = Conference.first
 > c.keyword_ids
 > []
 > c.keyword_ids = [ 1, 2, 3 ]
 => [ 1, 2, 3 ]
因此,我开始使用一个基本的复选框表单来尝试让事情正常运行:

在Railscast的帮助下:



成功了!现在我将返回并尝试一个更复杂的形式,以使它看起来像我想要的


谢谢你的帮助。我很感激

谢谢你的回复。我注释掉了“serialize:keyword,Array do”,但仍然得到相同的错误“authenticity_token”=>“GOJBEAHuPz2swO1zTXVgvgi9+JLBdIlSitnkThH6YRs=”,“conference”=>{“name”=>“test conference”,“address1”=>“address 1”,“address2”=>“address 2”,“city”=>“city”,“state”=>“state”=>test@example.com“,“web”=>“www.example.com”,“行业id”=>“2”,“industry_attributes”=>{“keywords”=>[“”],“id”=>“3”},“commit”=>“Update Conference”,“action”=>“Update”,“controller”=>“conferences”,“id”=>“1”}有什么想法吗?对不起,我忽略了参数。当你在Rails中使用嵌套属性时,你基本上是在说,“我想用……abc更新这个主对象。。。在本例中,您的意思是“我想用会议参数更新会议1,用行业属性哈希更新行业3”。“我怀疑这不是你的意图。相反,如果您希望在Conf和Industry以及Conf和关键字之间创建关联,那么就消除了的属性_。参数散列应该包含行业id和关键字id。首先,当我删除时,接受行业的嵌套属性,我收到一个错误“未知属性:行业属性”。因此,我从attr\u accessible中删除了:industry\u属性,并得到一个“无法批量分配受保护的属性:industry\u属性”错误。请检查表单。我假设表单的一部分显示了复选框,用户可以从中选择关键字。表单的这一部分是为了在industry_属性中返回“keywords”数组而编写的。如果检查HTML表单,复选框的名称可能会显示“conference[industry_attributes][keywords][]”。这是表单中需要更改的部分——它们应该是f.checkbox:keyword_id,而不是属性_。我使用了一个选择框,如Railscast在动态选择菜单上的一集中所示,但我认为我需要先简化它,使用复选框,然后再从那里开始。我将重新发布我的结果。谢谢
class Conference < ActiveRecord::Base
attr_accessible 
              :name, :address1, :address2, :city, :email,
              :web, :profile_id, :state_id, :conference, 
              :description, :date_start, :date_end, :venue, :venue_web, 
              :organizer, :organizer_web, :c_keywordables_attributes, 
              :industry_attributes, :industry_id,
              :industry, :keyword_ids

# Associations

belongs_to :state
belongs_to :industry
accepts_nested_attributes_for :industry

#Keywords
has_many :c_keywordables
has_many :keywords, through: :c_keywordables
accepts_nested_attributes_for :c_keywordables

serialize :keywords, Array

  def to_s
     state_id
  end

end
  <h2>edit <%= @conference.name %></h2>
        <%= form_for  @conference do |c| %>
            <%= c.label :name %>
            <%= c.text_field :name %>
            <%= c.label :address1 %>
            <%= c.text_field :address1 %>
            <%= c.label :address2 %>
            <%= c.text_field :address2 %>
            <%= c.label :city %>
            <%= c.text_field :city %>
            <%= c.label :state_id%>
            <%= c.collection_select :state_id, State.order(:name), :id, :name %>
            <%= c.label :email %>
            <%= c.text_field :email %>
            <%= c.label :web %>
            <%= c.text_field :web %>
       <hr width="80%">
            <%= c.label :industry_id %>
            <%= c.collection_select :industry_id, Industry.order(:name), :id, :name %>
      <br />
            <%= c.fields_for :industry, @industry do |ci| %>
             <%= ci.label :keyword_id %>
             <%= ci.grouped_collection_select :keywords, Industry.order(:name),
                :keywords, 
                :name, 
                :id, 
                :name, 
                {:include_blank => true}, { :multiple => true }
                 %>
       <% end %>
    <br />
<p><%= c.submit %></p>
<% end %>
def edit
    @conference = Conference.find(params[:id])
end

def update
    @conference = Conference.find(params[:id])
        if @conference.update_attributes(params[:conference])
            redirect_to action: "all", notice: 'conference was successfully updated.'
        else
            render action: "edit" 
        end
end
class Conference
  ...

  has_many :keywords, through: :c_keywordables

  ...

  serialize :keywords, Array do
    ...
  end
end
 has_many :c_keywordables
 has_many :keywords, through: :c_keywordables
 > c = Conference.first
 > c.keyword_ids
 > []
 > c.keyword_ids = [ 1, 2, 3 ]
 => [ 1, 2, 3 ]
<% Keyword.all.each do |keyword| %>
  <p>
   <%= check_box_tag "conference[keyword_ids][]", keyword.id, @conference.keyword_ids.include?(keyword.id) %>
   <%= keyword.name %>
  </p>
<% end %>