Answer the question
In order to leave comments, you need to log in
How to more competently implement the restriction of access to "unpublished" material?
Any news. The news has "statuses" - 0 or 1 (for example).
If "0", then the news is not published and only some roles have access, and if "1", then, on the contrary, it is published and all users have access to it.
I use CanCanCan and Rolify.
Can you please tell me how to implement it more competently in a RoR project?
So far, only checking the status and role of the user in the template is on my mind. But I also want ordinary users to display the default 404 error, saying that there is nothing for this ID.
Answer the question
In order to leave comments, you need to log in
In your case, I would use pundit scope
Like
this
def resolve
if user.admin?
scope.all
else
scope.where(status: 2)
end
end
news = policy_scope(News).find(params[:id])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question