Rails 5 using has_many 中不允许的参数,通过
Unpermitted parameters in Rails 5 using has_many, through
我的预约模型不断收到未经许可的参数。
这是我的模型
class Appointment < ApplicationRecord
belongs_to :client
belongs_to :trainer
end
class Trainer < ApplicationRecord
has_many :appointments
has_many :clients, through: :appointments
end
class Client < ApplicationRecord
has_many :appointments
has_many :trainers, through: :appointments
end
这是我的控制器,为了简洁起见,我只是列出了我的私有方法。
def appt_params
params.require(:appointment).permit(:appointment_date, client_id: [],
trainer_id: [])
end
错误表明培训师、客户使用了不允许的参数。
我的强参数方法是否遗漏了什么?
这是我的appointments/new视图
<%= form_for @appointment do |f| %>
<%= f.datetime_select :appointment_date %>
<%= f.collection_select :trainer, Trainer.all, :id, :first_name %>
<%= f.collection_select :client, Client.all, :id, :name %>
<%= f.submit %>
<% end %>
我将集合添加到我的 appt_params 方法中,但仍然出现相同的错误。我仍然掌握 Rails 的窍门,如有任何帮助,我们将不胜感激,谢谢!
既然你使用了关联,那么只有client_id和trainer_id就足够了,那些应该是整数形式而不是数组。
因此将您的强参数方法代码更改为:
def appt_params
params.require(:appointment).permit(:appointment_date, :client_id,
:trainer_id)
end
您的 applications/new 观点:
<%= form_for @appointment do |f| %>
<%= f.datetime_select :appointment_date %>
<%= f.collection_select :trainer_id, Trainer.all, :id, :first_name %>
<%= f.collection_select :client_id, Client.all, :id, :name %>
<%= f.submit %>
<% end %>
运行 是否与您有同样的问题,经过数小时的调试我能够解决它:
我是第一次在 Rails 中尝试与您相似的模型进行关联,因此大多数在线文档建议将外键定义为 client_ids:[]
假设您的 ID 是数组,而它们肯定是整数作为我的参数 where
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LEcwQ56xYJGpq2zIs6Cz0YbU7B7mBKRa6rhspVIxo9vEB5/UoFUvHYiN0UC0krTiIp+d0tzhit6DZT1Z8PmYYg==", "califica"=>{"text"=>"hi", "grade"=>"5", "user_id"=>"1", "party_id"=>"1"}, "commit"=>"Create Calification"}
我认为这是由于 f.collection_select
取了预期的一个值。因此,在使用 :user_id => []
、 user_id:[]
等数组的许可数小时后,我总是遇到 Unpermitted parameters.
错误
尝试了@Bharath 的回答(这是正确的),但它仍然无法正常工作,那时我意识到我的旧模型不是用参考制作的(ActiveModel::UnknownAttributeError (unknown attribute 'user_id' for Calification.):
)所以我不得不制作a references migration 添加外键,然后一切正常。
我的预约模型不断收到未经许可的参数。
这是我的模型
class Appointment < ApplicationRecord
belongs_to :client
belongs_to :trainer
end
class Trainer < ApplicationRecord
has_many :appointments
has_many :clients, through: :appointments
end
class Client < ApplicationRecord
has_many :appointments
has_many :trainers, through: :appointments
end
这是我的控制器,为了简洁起见,我只是列出了我的私有方法。
def appt_params
params.require(:appointment).permit(:appointment_date, client_id: [],
trainer_id: [])
end
错误表明培训师、客户使用了不允许的参数。 我的强参数方法是否遗漏了什么?
这是我的appointments/new视图
<%= form_for @appointment do |f| %>
<%= f.datetime_select :appointment_date %>
<%= f.collection_select :trainer, Trainer.all, :id, :first_name %>
<%= f.collection_select :client, Client.all, :id, :name %>
<%= f.submit %>
<% end %>
我将集合添加到我的 appt_params 方法中,但仍然出现相同的错误。我仍然掌握 Rails 的窍门,如有任何帮助,我们将不胜感激,谢谢!
既然你使用了关联,那么只有client_id和trainer_id就足够了,那些应该是整数形式而不是数组。
因此将您的强参数方法代码更改为:
def appt_params
params.require(:appointment).permit(:appointment_date, :client_id,
:trainer_id)
end
您的 applications/new 观点:
<%= form_for @appointment do |f| %>
<%= f.datetime_select :appointment_date %>
<%= f.collection_select :trainer_id, Trainer.all, :id, :first_name %>
<%= f.collection_select :client_id, Client.all, :id, :name %>
<%= f.submit %>
<% end %>
运行 是否与您有同样的问题,经过数小时的调试我能够解决它:
我是第一次在 Rails 中尝试与您相似的模型进行关联,因此大多数在线文档建议将外键定义为 client_ids:[]
假设您的 ID 是数组,而它们肯定是整数作为我的参数 where
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LEcwQ56xYJGpq2zIs6Cz0YbU7B7mBKRa6rhspVIxo9vEB5/UoFUvHYiN0UC0krTiIp+d0tzhit6DZT1Z8PmYYg==", "califica"=>{"text"=>"hi", "grade"=>"5", "user_id"=>"1", "party_id"=>"1"}, "commit"=>"Create Calification"}
我认为这是由于 f.collection_select
取了预期的一个值。因此,在使用 :user_id => []
、 user_id:[]
等数组的许可数小时后,我总是遇到 Unpermitted parameters.
尝试了@Bharath 的回答(这是正确的),但它仍然无法正常工作,那时我意识到我的旧模型不是用参考制作的(ActiveModel::UnknownAttributeError (unknown attribute 'user_id' for Calification.):
)所以我不得不制作a references migration 添加外键,然后一切正常。