我正在尝试将自定义属性添加到Storytime
gem。
以下是他们提供的强参数方法:https://github.com/CultivateLabs/storytime/blob/master/app/controllers/storytime/dashboard/posts_controller.rb
def post_params
byebug
post = @post || current_post_type.new(user: current_user)
permitted_attrs = policy(post).permitted_attributes
permitted_attrs = permitted_attrs.append(storytime_post_param_additions) if respond_to?(:storytime_post_param_additions)
params.require(current_post_type.type_name.tableize.singularize.to_sym).permit(*permitted_attrs)
end
这是我试图实施的内容。
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
将数组放在permitted_attrs
数组中,所以我尝试更改gem代码以改为使用concat
。
当我使用permitted_attr
而不是concat
记录append
内部byebug时,我得到了
[: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(*permitted_attrs)
时,只返回了我的自定义属性。