允许使用自定义键的参数

时间:2016-11-08 15:54:34

标签: ruby-on-rails model

我有一个名为TeamInvite的模型,我正在尝试为其创建team_invite_params方法。

当我为表创建迁移时,因为我想跟踪发件人和收件人,所以我选择了自定义字段名称

  

sender_id

  

recipient_id

不要混淆哪个用户是哪个。

无论如何,当我尝试并允许team_invite的参数时,我无法告诉许可方法

修改

  

" team_invite" = GT; {"用户" = GT;" 2"}

实际上应该是 recipient_id 下的内容。我尝试的所有方法要么不工作,要么最终都没有传递任何价值。

控制器

def create
  @team_invite = TeamInvite.new(team_invite_params)
end 

模型

  class TeamInvite < ApplicationRecord
    belongs_to :recipient, class_name: "User"
    belongs_to :sender, class_name: "User"
  end

PARAMS:

{"utf8"=>"✓", "authenticity_token"=>"0QLA9YiTu9nAf6QJLX5rg0DWa7CAMjqGOlUBICbcL8Ucs2DsCDgGBnhiDXPa/ot8DK0xwTR1D7MASu4/9tVj0w==", "team_invite"=>{"recipient_id"=>"2"}, "commit"=>"Save Team invite"}

查看(如果重要):

<%= form_for :team_invite, url: team_invites_path do |f| %>
  <%= f.select :recipient_id, User.all.collect { |p| [ p.name, p.id ] }, include_blank: false %>
  <%= f.submit %>
<% end %>

迁移:

class CreateTeamInvite < ActiveRecord::Migration[5.0]
  def change
    create_table :team_invites do |t|
      t.references :team, foreign_key: true
      t.integer :recipient_id
      t.integer :sender_id
      t.timestamps
    end
  end
end

1 个答案:

答案 0 :(得分:0)

你需要:

params.permit(:team_invites => [ :user ]) #or whatever other nested team_invite params you want to pass in.

strong_params的文档:https://github.com/rails/strong_parameters

相关问题