【发布时间】:2017-06-19 23:57:54
【问题描述】:
在 Rails 5 中嵌套参数时遇到错误:Unpermitted parameter: specialties
我有一个专家模型:
class Expertise < ApplicationRecord
has_many :buckets, through: :specialties
has_many :specialties
end
桶模型:
class Bucket < ApplicationRecord
has_many :expertises, through: :specialties
has_many :specialties
end
还有一个专业模型:
class Specialty < ApplicationRecord
belongs_to :expertise
belongs_to :bucket
end
我正在尝试允许用户编辑他或她的专长并调整与其关联的专长。 @buckets 是从控制器传入的,当前表单如下所示:
<%= form_for(expertise) do |f| %>
<%= f.fields_for :specialties do |s| %>
<%= s.collection_select :bucket_ids, @buckets, :id, :name, {}, { multiple: true, class: "input" } %>
<% end %>
<% end %>
我的表单基于this answer。
这是来自 ExpertisesController 的相关 sn-p:
def expertise_params
params.require(:expertise).permit(:user_id, :name, :rating, :description, specialties_attributes: [:id, :expertise_id, :bucket_id, :_destroy, bucket_ids: []])
end
下面是传入的参数:
Parameters: {"expertise"=>{"specialties"=>{"bucket_ids"=>["", "1"]}, "description"=>""}, "id"=>"97"}
Specialties 应该是一个数组,对吧?我不知道该怎么做。
目的是让用户轻松地从可用的存储桶 (@buckets) 中进行选择,以打开或关闭他或她的专长专长。因此,假设有 5 个可用存储桶,用户只能为该专长打开/关闭 5 个可能的专长。
【问题讨论】:
-
您是否已经尝试将
specialties_attributes切换为specialties? -
可以粘贴日志输出吗?
-
@ThiagoUruray,我试过了,但它抛出了一个错误:“Specialty expected, got Array”
-
@JCorcuera,从日志中添加了参数,你指的是这个吗?
标签: ruby-on-rails ruby forms nested-forms multiple-select