2011-03-02 41 views
2

我正在研究一個新的應用程序,並試圖自定義Devise在提交登錄表單後發送登錄或新註冊的用戶。爲什麼rails試圖重定向?

我想給用戶發送回在登錄行動是從,所以我創建了這些方法application_controller.rb

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    def store_location 
    session[:origin_url] = request.request_uri if request.get? and controller_name != "user_sessions" and controller_name != "sessions" 
    end 

    def redirect_back_or_default(default) 
    redirect_to(session[:origin_url] || default) 
    end 


    def after_sign_in_path_for(resource_or_scope) 
    [#https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in][1] 
    p "*********** About to check session" 
    if session[:origin_url] 
     p "*****************Redirecting after sign_in to " + session[:origin_url] 
     redirect_back_or_default(docs_path) 
     return #this syntax needs to be checked 
    else 
    super 
    end 
    return 
    end 
end 

這裏所謂的頁面是docs_controller,show動作代碼:

# GET /docs/1 
    # GET /docs/1.xml 
    def show 
    @doc = Doc.find(params[:id]) 
    store_location 

    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @doc } 
    end 
    end 

這裏是從服務器日誌的輸出:

"*********** About to check session" 
"*****************Redirecting after sign_in to /docs/3" 


Started GET "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at Wed Mar 02 01:12:16 +0100 2011 
    Processing by Devise::SessionsController#new as HTML 
    User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1 
Redirected to http://localhost:3000/docs/3 
Redirected to 
Completed in 119ms 

ActionController::ActionControllerError (Cannot redirect to nil!): 


Rendered /home/jon/.rvm/gems/[email protected]/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.7ms) 
Rendered /home/jon/.rvm/gems/[email protected]/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (955.7ms) 
Rendered /home/jon/.rvm/gems/[email protected]/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (986.3ms) 

我用Rails 3.0.5

感謝您的任何意見...

回答

3

after_sign_in_path_for(好像是這個名字說)應該返回的路徑,並沒有做任何redirect_to魔法。

既然你只是return到處都是沒有任何回報價值,Devise可能會得到你的nil

+0

這就是原因。我會清理'迴歸瘋狂'。 – CHsurfer 2011-03-02 07:16:55