我的错误如下
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/te communications_proposal/_form.html.erb 的小摘录
<% for user in User.find(:all) %>
<div>
<%= check_box_tag "proposal[users][]",user%>
<%= user.trading_name %>
</div>
<% end %>
有人可以告诉我哪里出错了吗?
最佳答案
看起来您应该将用户的 id
传递给 check_box_tag
,而不是记录本身,并在名称中使用 user_ids
复选框而不是用户
:
<%= check_box_tag "proposal[user_ids][]", user.id %>
引用资料:
关于ruby-on-rails - ActiveRecord::关联类型不匹配。有很多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13675956/