views:

391

answers:

2

I want a before filter like "must_have_permission_to_write" that when called if user hasn't permission to write renders a message saying "you can't do that!" and return.

Problem is I'm getting "can only render or redirect once per action" of course... how can I stop the execution in the before filter? thanks

A: 

Just add the and return, like doing:

before_filter :must_have_permission_to_write

def must_have_permission_to_write
  redirect_to login_path and return unless current_user
end
khelll
+2  A: 

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

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

Then, create an action for access denied somewhere, add the route, and put whatever you want in the template.

Ben
This "and return false" thing does seem a little dangerous to be using as a pattern. If redirect_to returns nil, then nil is returned instead. Putting "return false" on a separate line is probably a better way to approach this.
tadman
Good call, fixed in the answer
Ben