扩展宝石的强大参数

时间:2015-11-23 12:51:24

标签: ruby-on-rails strong-parameters

我正在尝试将自定义属性添加到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)时,只返回了我的自定义属性。

0 个答案:

没有答案