Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters passed into a before_filter method call? [duplicate]

In my application_contorller I have the method:

def authorize(message = "Please Login")
    if current_user.nil?    
        redirect_to :root, alert: message 
        return
    end
end

And I want to be able to put a custom message in if needed so that I can call this method from many different controllers in a before_filter.

For example in another controller I have:

before_filter :authorize, except: [:index,:show]

How can I pass the message param into the :authorize method call in the before_filter in order to show a different message?

like image 549
Deekor Avatar asked Mar 04 '26 09:03

Deekor


1 Answers

There isn't a way to pass parameters to a request callback when you define it using a symbol. You can instead use a block:

before_filter(only: [:index, :show]) { authorize('You forgot to login') }
like image 127
coreyward Avatar answered Mar 06 '26 00:03

coreyward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!