使用activeadmin gem创建has_many关联的最简单方法是什么?

时间:2012-05-29 18:41:05

标签: ruby-on-rails ruby-on-rails-3 gem activeadmin

我只使用activeadmin gem:

rails生成active_admin:资源团队

rails生成active_admin:资源播放器

应用程序/模型

class Team < ActiveRecord::Base
  has_many :players
  attr_accessible :description, :name
end

class Player < ActiveRecord::Base
  belongs_to :team
  attr_accessible :description, :name
end

应用程序/控制器/ players_controller

class PlayersController < ApplicationController
    def create
        @team = Team.find(params[:team_id])
        @player = @team.players.create(params[:player])
    end
end

应用程序/管理/

  ActiveAdmin.register Team do
  end

  ActiveAdmin.register Player do
  end

当我尝试使用管理界面创建新播放器时,我收到:

  

ActiveModel :: MassAssignmentSecurity :: Admin :: PlayersController中的错误#create

Can't mass-assign protected attributes: team_id

我知道我必须做更多的事情,但是什么呢?非常感谢。

1 个答案:

答案 0 :(得分:1)

您需要将team_id添加到attr_accessible:

class Player < ActiveRecord::Base
    belongs_to :team
    attr_accessible :description, :name, :team_id
end