Skip to content

params_filter introspection #213

Answered by palkan
Spone asked this question in Q&A
Jul 15, 2022 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

I would suggest using a plain old Ruby method in the policy class for that:

class PostPolicy < ApplicationPolicy
  def permitted_attributes
    %i[title]
  end

  def permitted_attribute?(name)
    permitted_attributes.include?(name)
  end

  # you can re-use this method in the params scope
  params_filter { _1.permit(*permitted_attributes)  }
end

# in your view
- if policy_for(@post).permitted_attribute?(:title)
  = f.text_field :title 

Alternatively, you can use scopes (if you need different contexts):

class UserPolicy < ApplicationPolicy
  scope_for :permitted_params do
    %i[name email]
  end

  scope_for :permitted_params, :update do
    %i[name]
  end
end

authorized_scope(user, type

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Spone
Comment options

@Spone
Comment options

Answer selected by Spone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants