Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute an action if the before_action returns false

I know that with the following code:

before_action :signed_in?, only: [:new]

the action new will be executed if the signed_in? returns true, but instead if I want the new action to be executed when signed_in? returns false what do I have to do? Do I have to create a new method called, for instance, not_signed_in??

Here it is my signed_in? method

def signed_in?
  !@current_user.nil?
end
like image 299
zer0uno Avatar asked Nov 21 '13 12:11

zer0uno


1 Answers

before_action doesn't work as you think - it doesn't prevent action to be executed if callback returns false. I would solve your problem in a little bit different manner, for example:

before_action :redirect_to_root, :if => :signed_in?, :only => :new

# ...
private
def redirect_to_root
  redirect_to root_path
end
like image 64
Marek Lipka Avatar answered Oct 20 '22 04:10

Marek Lipka