Rails 形成 select 加入 table 产生 ActiveRecord::AssociationTypeMismatch

Rails form select for join table produces ActiveRecord::AssociationTypeMismatch

我正在尝试为包含示例项目的数据库设置数据输入。项目可以是系列的一部分,有多个策展人等。

我要么得到显示的系列表格:

<%= f.collection_select :series, Series.order('word asc'), :id, :word, {}, {:class => 'select-field-style'} %>

样式正确,但提交后我得到

ActiveRecord::AssociationTypeMismatch in Admin::ProjectsController#update
Series(#2212122800) expected, got String(#2183812080)

<%= f.select :series, Series.all.collect{ |p| [ p.word, p.id ]}, :include_blank => true, :class => "select-field-style" %>

CSS 未应用,但我设法得到一个空白选项。 提交产生相同的响应。

策展人

<%= f.select :curators, options_for_select(Author.all.map{|c| [c.name, c.id]}, f.object.curators), {}, {:class => "form-control selectpicker", :include_blank => true, :multiple => true} %>

使用我的自定义样式生成一个多 select 字段并在提交时出现类似的错误

ActiveRecord::AssociationTypeMismatch in Admin::ProjectsController#update
Author(#2214244880) expected, got String(#2183812080)

我需要能够将我自己的 CSS 应用到 select 或者,具有 单一 多个空白。表格不起作用,没有版本允许所有选项。

这些关系是通过join-table建立的。我的模型是:

项目模型

class Project < ActiveRecord::Base
  attr_accessible :series, :curators

# Associations 
  has_and_belongs_to_many :curators, :class_name => "Author", :join_table => "projects_curators"
  has_and_belongs_to_many :series, :join_table => "projects_series" 
end

系列型号

class Series < ActiveRecord::Base
  attr_accessible :word

# Associations
  has_and_belongs_to_many :projects, :join_table => "projects_series" 

结束

策展人加入table

class ProjectsCurator < ActiveRecord::Base  
  attr_accessible :project_id, :author_id

# Associations
  belongs_to :project
  belongs_to :author
end

作者模型

class Author < ActiveRecord::Base
  attr_accessible :name

# Associations
  has_and_belongs_to_many :projects, :join_table => "projects_curators"
end

更新

感谢@xploshioOn 的回答,我现在能够获得正确的 selection 选项。

<%= f.select(:series, Series.order('word asc').map{|s| [s.word, s.id]}, {:include_blank => false}, {:class => 'select-field-style'}) %>

呈现带有自定义 CSS 的 selection 菜单。

<%= f.select(:curators, Author.order('name asc').map{|s| [s.name, s.id]}, {:include_blank => true}, {:class => "select-field-style", :multiple => true}) %>

呈现带有自定义 CSS、多个 selection 选项和空白的 selection 菜单。

不幸的是,在提交时我仍然得到:

ActiveRecord::AssociationTypeMismatch in Admin::ProjectsController#update
Series(#2285826200) expected, got String(#2257413120)

Author(#2286926340) expected, got String(#2257413120)

奇怪的是:

<%= f.select(:curators, Author.order('name asc').map{|s| [s.name, s.id]}, {:include_blank => true}, {:class => "select-field-style", :multiple => true}) %>

有预期的结果,而

<%= f.select(:curators, Author.order('name asc').map{|s| [s.name, s.id]}, {:include_blank => true, :multiple => true}, {:class => "select-field-style"}) %>

为什么?我怎样才能让提交生效?

更新 2

系列表格和大多数其他表格现在可以按预期工作。通过 join tables 和 class names 与 habtm 关系相关的形式还不能工作。

一些上下文:在这些情况下,我需要将项目与组织者、策展人、支持者等相关联。关系存在于以下模型之间:

Project.rb - projects_curator.rb - author.rb

Project.rb - projects_supporter.rb - institution.rb

在这两种情况下,我都有不同版本的 join-table,并且每次我都在作者和机构模型中声明了 class/alias。正如之前所写。

使用上面提到的 select 表单,我在提交时收到以下错误

Started PUT "/admin/projects/1" for 127.0.0.1 at Wed Dec 05 20:03:31 +0100 2018
Processing by Admin::ProjectsController#update as HTML
  Parameters: {"project"=>{"place"=>"Moon", "hero_id"=>"1", "name"=>"Rocket", "year"=>"1492", "description"=>"And now the news.", "curators"=>["", "14"], "category_id"=>"2", "project_id"=>"1", "series_ids"=>["", "3"]}, "utf8"=>"✓", "commit"=>"Save", "authenticity_token"=>"some_token", "id"=>"1"}
  Project Load (0.3ms)  SELECT `projects`.* FROM `projects` WHERE `projects`.`id` = ? LIMIT 1  [["id", "1"]]
  SQL (0.1ms)  BEGIN
   (0.1ms)  ROLLBACK
Completed 500 Internal Server Error in 3ms

ActiveRecord::AssociationTypeMismatch (Author(#2302596480) expected, got String(#2274315260)):
  app/controllers/admin/projects_controller.rb:28:in `update'


  Rendered /Users/account/.rvm/gems/ruby-version@app/gems/actionpack-x.x.x/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.6ms)
  Rendered /Users/account/.rvm/gems/ruby-version@app/gems/actionpack-x.x.x/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
  Rendered /Users/account/.rvm/gems/ruby-version@app/gems/actionpack-x.x.x/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (8.3ms)

跟踪的最后一部分是:

activerecord (x.x.x) lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
activerecord (x.x.x) lib/active_record/associations/collection_association.rb:318:in `replace'
activerecord (x.x.x) lib/active_record/associations/collection_association.rb:318:in `each'
activerecord (x.x.x) lib/active_record/associations/collection_association.rb:318:in `replace'
activerecord (x.x.x) lib/active_record/associations/collection_association.rb:41:in `writer'
activerecord (x.x.x) lib/active_record/associations/builder/association.rb:51:in `curators='
activerecord (x.x.x) lib/active_record/attribute_assignment.rb:85:in `send'
activerecord (x.x.x) lib/active_record/attribute_assignment.rb:85:in `assign_attributes'
activerecord (x.x.x) lib/active_record/attribute_assignment.rb:78:in `each'
activerecord (x.x.x) lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
activerecord (x.x.x) lib/active_record/persistence.rb:212:in `update_attributes'
activerecord (x.x.x) lib/active_record/transactions.rb:295:in `with_transaction_returning_status'
activerecord (x.x.x) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (x.x.x) lib/active_record/transactions.rb:208:in `transaction'
activerecord (x.x.x) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (x.x.x) lib/active_record/persistence.rb:211:in `update_attributes'
app/controllers/admin/projects_controller.rb:28:in `update'
actionpack (x.x.x) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (x.x.x) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (x.x.x) lib/abstract_controller/base.rb:167:in `process_action'

来自projects_controller的相对动作是标准的:

def update
    @project = Project.find(params[:id])
    if @project.update_attributes(params[:project])
      flash[:notice] = 'project was successfully updated.'
      redirect_to :action => 'show', :id => @project
    else
      @page_title = 'Edit project'
      render :action => 'edit'
    end
  end 

您可以指定 class 并在 select 上包含空白,像这样

<%= f.select(:series, Series.order('word asc').map{|s| [s.word, s.id]}, {:include_blank => true}, {:class => 'select-field-style'})  %>

感谢xploshioOn的回答,我终于找到了不匹配的解决方案。

我必须在所有形式 select 字段中使用 id 的复数形式,例如:

series_ids, curator_ids.

因此,我的最后一行脚本是:

<%= f.select(:curator_ids, Curator.order('name asc').map{|s| [s.name, s.id]}, {:include_blank => true}, {:class => 'select-field-style'})  %>