问题
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