2016-12-05 46 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