Ruby on rails 扩展gem&x27;s强参数
我正在尝试向Ruby on rails 扩展gem&x27;s强参数,ruby-on-rails,strong-parameters,Ruby On Rails,Strong Parameters,我正在尝试向Storytimegem提供的模型添加自定义属性 以下是他们提供的强参数方法: 以下是我试图实现的 Storytime::Dashboard::DashboardController.class_eval do before_action :parse_input, only: [:update] def storytime_post_param_additions attrs = [{:spree_product_ids => []}] attrs
Storytime
gem提供的模型添加自定义属性
以下是他们提供的强参数方法:
以下是我试图实现的
Storytime::Dashboard::DashboardController.class_eval do
before_action :parse_input, only: [:update]
def storytime_post_param_additions
attrs = [{:spree_product_ids => []}]
attrs
end
def parse_input
params[:spree_product_ids] = params[:spree_product_ids].split(',').map(&:to_i)
end
end
但是,我发现append
将数组放入允许的属性数组中,因此我尝试将gem代码改为使用concat
当我用concat
而不是append
将allowed\u attr
记录在ebug内部时,我得到了
[:title, :draft_content, :draft_version_id, :excerpt, :featured_media_id, :secondary_media_id, :slug, :published_at_date, :notifications_enabled, :published_at_time, {:tag_list=>[]}, :published, {:spree_product_ids=>[]}]
但是当我尝试params.permit(*allowed_attrs)
时,只返回了我的自定义属性。结果是我将输入命名错误(没有product_blog名称空间),因此,params.require(:product_blog)会导致从哈希中删除名称空间之外的所有内容。我将视图更改为,现在它可以工作了
<%= f.association :spree_products, input_html: { name: "#{@post.class.name.split('::').last.tableize.singularize}[spree_product_ids][]", class: 'chosen-select tags-input', multiple: true } %>
<%= f.association :spree_products, input_html: { name: "#{@post.class.name.split('::').last.tableize.singularize}[spree_product_ids][]", class: 'chosen-select tags-input', multiple: true } %>