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)

プレイヤーがセーブしない理由は?もともとparams.requireにはなかっplayer_idsたのですが、無駄に追加しました

4

1 に答える 1

23

試してみてください:

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