how to execute an action if the before_action returns false

三世轮回 提交于 2019-12-04 23:57:38

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
before_action :new, unless: -> { signed_in? }

alltough i think its better to redirect in the action which was called.

def your_action_called
  redirect_to :new unless signed_in?
  [...code ...]
end

If you want to cover all other methods with
before_action :signed_in?
except new action, you'b better to use :except instead of :only
like this:
before_action :signed_in?, except: :new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!