2013-05-16 34 views
1

嗨,大家這可能是簡單的事情,但我無法在任何地方在Google上找到答案。 我在我的Rails應用程序上有一個歡迎頁面,當您使用設計器登錄時,我想將用戶從該頁面直接重定向到帖子頁面,該頁面與我正在使用的代碼正常工作。唯一的問題是我不能再次訪問該頁面,一旦你登錄,因爲它顯然仍然重定向。有誰知道登錄時是否有重定向的方式,但是如果可能的話,仍然允許你返回到歡迎頁面?如何防止用Devise登錄錯誤的重定向?

class WelcomeController < ApplicationController 
    def index 
    if user_signed_in? 
     redirect_to :controller=>'posts', :action=>'index' 
    end 
    end 
end 

class PostsController < ApplicationController 
    before_filter :authenticate_user! 

    # GET /statuses 
    # GET /statuses.json 
    def index 
    @posts = Post.order('id DESC').paginate(:per_page => 5,:page => params[:page]) 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @posts } 
    end 
    end 
end 

謝謝。

+0

你可以顯示你在PostsController#index處有什麼嗎? – poseid

回答

0

這是將signed_in用戶重定向到您的Posts頁面的完全錯誤方式。

制定對如何做到這一點非常良好的文檔 - https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in

您應該添加以下在ApplicationController

def after_sign_in_path_for(resource) 
    stored_location_for(resource) || posts_path 
end 
+0

謝謝,你知道我會投入資源嗎?感謝您的幫助 – user2225112

+0

您的意思是放入您的'config/routes.rb'?那麼,沒有什麼:) – kiddorails

+0

很感謝mil – user2225112