2

我是以下错误

ActiveRecord::AssociationTypeMismatch in ProposalsController#create

User(#78682120) expected, got String(#68929150)
Rails.root: /home/james/app

Application Trace | Framework Trace | Full Trace
app/controllers/proposals_controller.rb:76:in `new'
app/controllers/proposals_controller.rb:76:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"0qTzQ/KMA2Ch60aajSg265tThfCTBCB7w0rS8nD4Qwg=",
 "proposal"=>{"phones"=>"",
 "broadband"=>"",
 "fixed_line"=>"",
 "group_calling"=>"",
 "frequent_txt"=>"",
 "frequent_data"=>"",
 "type"=>"TelecommunicationsProposal",
 "contact_type"=>"",
 "extra_options"=>"",
 "current_provider"=>"",
 "closing_date"=>"03/12/2012",
 "users"=>["#<User:0x961c9e4>"]},
 "commit"=>"Create Proposal"}

我正在尝试建立一种关系,使用户有很多提案,并且提案可以有很多用户。

我有它可以为每个用户获得建议,但也需要反过来。

提案.rb

 class Proposal < ActiveRecord::Base
      has_many :users
    ...

电信提案.rb

class TelecommunicationsProposal < Proposal
  belongs_to :users
  after_create :proposal_creation
  ...

ProposalsController.rb 错误在第 76 行中断,这是 Proposal.new 创建

 def create
    @proposal = Proposal.new(params[:proposal])

    if current_user
     @user = current_user

来自 app/views/telecommunications_proposal/_form.html.erb 的小节选

 <% for user in User.find(:all) %>  
    <div>  
      <%= check_box_tag "proposal[users][]",user%>  
      <%= user.trading_name %>  
    </div>  
<% end %> 

有人可以告诉我哪里出错了吗?

4

1 回答 1

3

看起来您应该将id用户的 传递给check_box_tag,而不是记录本身,并user_ids在复选框的名称中使用,而不是users

<%= check_box_tag "proposal[user_ids][]", user.id %>

参考:

于 2012-12-03T01:55:44.447 回答