0

我只是使用activeadmin gem:

rails generate active_admin:resource 团队

rails generate active_admin:resource 玩家

应用程序/模型

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

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

4

1 回答 1

1

您需要将 team_id 添加到 attr_accessible:

class Player < ActiveRecord::Base
    belongs_to :team
    attr_accessible :description, :name, :team_id
end
于 2012-05-29T19:33:03.397 回答