【问题标题】:What is the easiest way to create has_many association using activeadmin gem?使用 activeadmin gem 创建 has_many 关联的最简单方法是什么?
【发布时间】:2012-05-29 18:41:05
【问题描述】:

我只是使用 activeadmin gem:

rails 生成 active_admin:resource 团队

rails 生成 active_admin:resource Players

应用程序/模型

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

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

app/controller/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

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

【问题讨论】:

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


    【解决方案1】:

    你需要将 team_id 添加到 attr_accessible:

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

    【讨论】:

    • 最简单,就像我说的。谢谢。
    猜你喜欢
    • 2017-04-05
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多