连接表的Rails形式选择产生ActiveRecord :: AssociationTypeMismatch

时间:2018-12-05 09:14:40

标签: ruby-on-rails forms ruby-on-rails-3 select multi-select

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

我得到的序列表显示为:

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

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

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

我需要能够将自己的 CSS 应用于选择器,并且能够多个空白。表格不起作用,没有版本允许所有选项。

这些关系是通过联接表建立的。我的模型是:

项目模型

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" 

结束

策展人联接表

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的回答,我现在能够正确选择正确的选择选项。

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

使用自定义CSS呈现选择菜单。

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

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

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

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

系列表格和大多数其他表格现在可以按预期工作。通过联接表和类名与habtm关系相关的形式尚不可用。

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

Project.rb-projects_curator.rb-author.rb

Project.rb-projects_supporter.rb -stitution.rb

在两种情况下,我都有不同的联接表版本,并且每次我都有在作者和机构模型中声明的类/别名。就像以前写的一样。

使用上述选择表格,提交后出现以下错误

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 

2 个答案:

答案 0 :(得分:1)

您可以在选择内容上指定类和包含空白,

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

答案 1 :(得分:0)

由于xploshioOn的回答,我终于找到了解决不匹配的方法。

我必须在所有形式选择字段中使用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'})  %>