加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – rails对象期望得到字符串

发布时间:2020-12-17 03:05:01 所属栏目:百科 来源:网络整理
导读:我见过类似的多个问题,但没有一个对我有用. 我有一个团队模型: class Team ActiveRecord::Base has_one :p1,:class_name = "Player",:foreign_key = 'player_id',:validate = true has_one :p2,:validate = trueend 在我的团队的_form.html.erb中,我指的是
我见过类似的多个问题,但没有一个对我有用.

我有一个团队模型:

class Team < ActiveRecord::Base
  has_one :p1,:class_name => "Player",:foreign_key => 'player_id',:validate => true
  has_one :p2,:validate => true
end

在我的团队的_form.html.erb中,我指的是玩家

<%= f.collection_select :p1,Player.all,:id,:name %>

但是,在表单提交时,我看到错误:

Player(#28401456) expected,got String(#14111904)

Application Trace | Framework Trace | Full Trace
app/controllers/teams_controller.rb:47:in `new'
Parameters:
{"utf8"=>"?","authenticity_token"=>"GSIcEvROFnvgGWT4HvE2VNqRw4NxU1J8iAw/WhZeRLk=","team"=>{"p1"=>"1"},"commit"=>"Create Team"}

这是代码在线

def create
  @team = Team.new(params[:team])
  .....
end

有什么想法吗?

解决方法

最后,这工作:

<%= f.collection_select :p1_id,:name %>

这是魔术:
我的迁移有t.references p1,并在数据库中创建了一列p1_id.
提交表单时,rails正在寻找以下内容填写引用的ID:

def create
  @team = Team.new(params[:team])
  .....
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读