gpt4 book ai didi

ruby-on-rails - ActiveRecord::AssociationType 不匹配。 has_many 关系

转载 作者:数据小太阳 更新时间:2023-10-29 08:50:43 26 4
gpt4 key购买 nike

我是下面的错误

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在 Proposal.new 创建的第 76 行出现错误

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

谁能告诉我哪里出错了?

最佳答案

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

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

引用:

关于ruby-on-rails - ActiveRecord::AssociationType 不匹配。 has_many 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13675956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com