【问题标题】:Rails form select for join table produces ActiveRecord::AssociationTypeMismatchRails 表单选择连接表产生 ActiveRecord::AssociationTypeMismatch
【发布时间】:2018-12-05 09:14:39
【问题描述】:

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

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

<%= 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 应用到选择器上,能够singlemultipleblank 。表格不起作用,没有版本允许所有选项。

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

项目模型

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 -institution.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)

trace的最后一部分是:

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 

【问题讨论】:

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


    【解决方案1】:

    您可以在选择时指定类和包含空白,如下所示

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

    【讨论】:

    • 非常感谢您的意见。根据您的建议,我能够正确选择所有选项,但仍会触发 Admin::ProjectsController#update 中的“AssociationTypeMismatch”。我会用必要的信息更新我的问题。
    • 您能否将该操作的代码添加到问题和完整错误中?
    • 我添加了更多细节。我正在查看我使用不同的连接表以不同方式连接两个模型的事实。明天我将尝试从 habtm 和 join-table 转到有许多第三个模型。我会报告回来,但任何提示都非常感谢。事实上,在其余的表格中,这些表格都是 habtm,表格并没有像现在的系列那样突出显示 select 表格​​中的现有数据集。
    【解决方案2】:

    感谢 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'})  %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 2014-04-25
      相关资源
      最近更新 更多