2016-12-05 101 views
0

我正在使用Octokit来登录。redirect_to没有工作到救援块

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 

的root_path是在配置

root to: 'home#new' 

救援ES执行,然而redirect_to的不工作,它返回到相同的视图的主要方法。注意:我在很多帖子中看到,将修复它,但它没有

+0

从root_path中删除':'并确保root_path在config/routes.rb文件中定义。此外,'返回false'停止进一步的执行。 – bkunzi01

+0

@ bkunzi01我拼错了。我更新了这篇文章。根被定义并且root_path被正确定义 –

回答

1

您的代码调用redirect_to方法,但救援块随后返回nil。相反,结合重定向并返回到一个单一的语句:

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

其实你并不需要回报可言,除非有方法本声明之后的东西。这是因为在Ruby中,最后一条语句被隐式返回。

+0

我试过了,它没有工作。问题是,该代码是一个辅助方法,而不是在方法控制器? –

+0

我更新了帖子,以澄清@mysmallidea –

+0

如果没有用户,Octokit是否确实会引发异常? – mysmallidea