4

我有一个拥有并且属于许多关系设置,如下所示:

has_and_belongs_to_many :players, :class_name => "User" # In app/models/team.rb
has_and_belongs_to_many :teams                          # In app/models/user.rb

在已经创建用户之后,我正在使用表单来创建团队。这是我的团队控制器:

def create
  @team = Team.create(team_params)
end

private
def team_params
  params.require(:team).permit(:name,:captain,:season,:year,:active,:player_ids)
end

除了玩家(用户)之外,一切都保存了下来。这是控制台上的输出:

Started POST "/teams" for 127.0.0.1 at 2013-10-20 01:46:04 -0400
Processing by TeamsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jhNUgdfSVNxBgpZkmvjSQg/7DGsV1ts+Y1a1xWQ6A1Y=", "team"=>{"name"=>"uuuuu", "captain"=>"18", "season"=>"Summer", "year"=>"2013", "player_ids"=>["", "18"]}, "button"=>""}
  User Load (1.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 18 ORDER BY "users"."id" ASC LIMIT 1
Unpermitted parameters: player_ids
   (0.2ms)  BEGIN
  SQL (3.4ms)  INSERT INTO "teams" ("captain", "created_at", "name", "season", "updated_at", "year") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["captain", 18], ["created_at", Sun, 20 Oct 2013 05:46:04 UTC +00:00], ["name", "uuuuu"], ["season", "Summer"], ["updated_at", Sun, 20 Oct 2013 05:46:04 UTC +00:00], ["year", 2013]]
   (4.2ms)  COMMIT
Redirected to http://lvh.me:3000/admin
Completed 302 Found in 23ms (ActiveRecord: 9.3ms)

知道玩家为什么不保存吗?本来player_idsparams.require里面是没有的,但是我加了也没用

4

1 回答 1

23

尝试:

params.require(:team).permit(:name,:captain,:season,:year,:active,player_ids: [])
于 2013-10-21T14:58:37.480 回答