A
A
Anton2016-04-20 22:15:22
Ruby on Rails
Anton, 2016-04-20 22:15:22

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

1 answer(s)
A
Andrey Demidenko, 2016-04-20
@hummingbird

In your case, I would use pundit scope
Like this

def resolve
  if user.admin?
    scope.all
  else
    scope.where(status: 2)
  end
end

And in the controller, let's say something like this
news = policy_scope(News).find(params[:id])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question