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

ruby-on-rails – 无法让accepted_nested_attributes_for深入两

发布时间:2020-12-17 02:05:06 所属栏目:百科 来源:网络整理
导读:我有三款游戏团队玩家和我希望能够提交以下内容以添加游戏以及这些团队中的多个团队和玩家. {"game"={"name"="championship","teams_attributes"=[ {"result"="won","players_attributes"={"name"="Bob"}},{"result"="lost","players_attributes"={"name"="T
我有三款游戏>团队>玩家和我希望能够提交以下内容以添加游戏以及这些团队中的多个团队和玩家.

{"game"=>{"name"=>"championship","teams_attributes"=>[
    {"result"=>"won","players_attributes"=>{"name"=>"Bob"}},{"result"=>"lost","players_attributes"=>{"name"=>"Tad"}}]}}

这是我的模特:

class Game < ActiveRecord::Base
   attr_accessible  :name,:teams_attributes,:players_attributes

   # Associations
   has_many :teams,:inverse_of => :game
   has_many :players,:through => :teams

   accepts_nested_attributes_for :teams
   accepts_nested_attributes_for :players
end

class Team < ActiveRecord::Base
     attr_accessible :game_id,:result,:players_attributes

     # Associations
     belongs_to :game,:inverse_of => :teams
     has_many :players,:inverse_of => :team

     accepts_nested_attributes_for :players
end

class Player < ActiveRecord::Base
  attr_accessible :team_id,:name

  # Associations
  belongs_to :team,:inverse_of => :players
  # belongs_to :game,:through => :team (causes error,doesn't fix)

end

我可以在添加游戏时添加两个团队,但我无法添加游戏,在每个团队中添加两个团队和玩家.我的设置有问题吗?尝试添加时,我不断收到“无法将字符串转换为整数”错误.这是我刚刚玩游戏时遇到的错误>团队,但是当我添加了inverse_of东西时修复了.

谢谢!

解决方法

想出来……我的哈希设置是一个问题.
正在使用:

{"game"=>{"name"=>"championship","teams_attributes"=>[
{"result"=>"won","players_attributes"=>{"name"=>"Tad"}}]}}

但应该是(在players_attributes周围括号):

{"game"=>{"name"=>"championship","players_attributes"=>[{"name"=>"Bob"}]},"players_attributes"=>[{"name"=>"Tad"}]}]}}

(编辑:李大同)

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

    推荐文章
      热点阅读