Rails: place and break in front of the filter

I want that before the filter, like "must_have_permission_to_write", when called, if the user does not have write permission, the message "you can not do this!" and come back.

The problem is that I get "can only render or redirect once per action", of course ... how can I stop execution in the front filter? thanks

+3
source share
3 answers

I think the easiest answer is to add a redirect and return false to your method must_have_permission_to_write.

def must_have_permission_to_write
  unless current_user.has_permission?(something)
    redirect_to access_denied_path 
    return false
  end
end

, , .

+3

, :

  • . , , , , . , -, , , .
  • " " - , . (.. ), " " , .
  • , - " ", . - -, , 404. , "" .
0

Just add and returnfor example:

before_filter :must_have_permission_to_write

def must_have_permission_to_write
  redirect_to login_path and return unless current_user
end
-1
source

Source: https://habr.com/ru/post/1718725/


All Articles