redirect_to is not working into rescue block

試著忘記壹切 提交于 2019-12-12 03:04:50

问题


I am using Octokit so as to login.

helper_method :user

def show
end 

def user
  client = Octokit::Client.new(access_token: session[:access_token])
  begin
    @user = client.user
  rescue => e
    redirect_to root_path
    return
  end
end 

The root_path is in the config

  root to: 'home#new'

The rescue es executed, however the redirect_to isn't working, it return to the same view as the main method. Note: I read in many post that putting return fix it, nevertheless it didn't


回答1:


Your code is calling the redirect_to method, but the rescue block is subsequently returning nil. Instead, combine the redirect and return into a single statement:

client = Octokit::Client.new(access_token: session[:access_token])
begin
  @user = client.user
rescue => e
  redirect_to root_path and return
end

In fact you don't need the return at all, unless there is something after this statement in the method. This is because in Ruby the last statement is implicitly returned.



来源:https://stackoverflow.com/questions/40981615/redirect-to-is-not-working-into-rescue-block

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