2011-12-16 31 views
4

這是它應該如何工作的: 我登錄到管理面板,轉到cars/new並填寫字段,按create,我應該有一個新的汽車在我的列表中。 www.autozeep.comrails 3 warden NameError(uncaught throw`warden'):

的事情是,它直到我按創建按鈕來創建新的車,服務器日誌變爲OK表明這一點:

NameError (uncaught throw `warden'): 
    app/controllers/application_controller.rb:9:in `login_required' 
    app/middleware/flash_session_cookie_middleware.rb:17:in `call' 
在開發模式

這個工作正常,在生產服務器上模式不是,它是相同的代碼,沒有任何改變。 更多的服務器日誌:http://pastie.org/3028350

application_controller 

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    # filter 

    def login_required 
    return true if authenticated? 
    warden.authenticate! 
    end 

users_controller:http://pastie.org/3028586

我可以編輯的汽車,它工作正常,所以更新和編輯功能從cars_controller都OK,我檢查了新創建函數從cars_controller但我無法處理任何會讓我知道發生了什麼的事情。 Cars_controller:http://pastie.org/3028452

請幫助,我有這個程序allready運行和客戶端si等待這個問題才解決。非常感謝你。

編輯

NameError in CarsController#create 

uncaught throw `warden' 

Rails.root: /u/apps/zeepauto/releases/20111123173432 
Application Trace | Framework Trace | Full Trace 

app/controllers/application_controller.rb:9:in `login_required' 
app/middleware/flash_session_cookie_middleware.rb:17:in `call' 

ENV DUMP 
... 
.... 
rack.url_scheme: "http" 
rack.version: [1, 0] 
warden: Warden::Proxy:-621456458 @config={:default_strategies=>{:_all=>[:database]}, :failure_app=>UsersController, :default_scope=>:default, :scope_defaults=>{}, :intercept_401=>true} 
warden.options: {:attempted_path=>"/cars", :action=>"unauthenticated"} 

我得到這個錯誤,只有當我添加新的車,我可以編輯的汽車,新聞,聯繫人。除汽車外的所有東西

問題解決了

這個問題通過一些jQuery庫造成的,我使用dynamic_form以這種形式,所以當我在接下來的select_box選擇車名出現所選汽車的型號。檢查問題(我的老師,我不會想到它自己),我們看到,當我選擇汽車時,在日誌中運行名爲「dynamic_carmodels」的過程來更新carmodels列表,並且此時會話密鑰是由另一個更改,通常如果會話密鑰更改,我登錄時啓動的會話不再有效,這就是爲什麼我得到一個「未經驗證的錯誤」。仍然不知道什麼jquery確實是造成這個問題,但最終我得到了這個解決,這不是因爲監獄配置。

+0

你可以發佈你的Gemfile。 – daniel 2011-12-18 22:42:59

+0

它在https://github.com/rmagnum2002/zeepauto – rmagnum2002 2011-12-18 22:45:50

回答

10

好吧我會向你解釋爲什麼這個異常發生得非常仔細,但我無法爲你解決它。

監獄長守衛您的應用程序使用catch(:區長):

# Invoke the application guarding for throw :warden. 
# If this is downstream from another warden instance, don't do anything. 
# :api: private 
def call(env) # :nodoc: 
    return @app.call(env) if env['warden'] && env['warden'].manager != self 

    env['warden'] = Proxy.new(env, self) 
    result = catch(:warden) do 
     @app.call(env) 
    end 

您的應用程序獲取@ app.call(ENV),如果你的應用程序拋出所謂的(:監獄長塊,你可以看到這)它被抓住了。這是怎麼扔,抓作品,這裏有一個例子:

def authenticate!() 
    throw :warden 
end 

catch(:warden) do 
    puts "Calling authenticate!" 
    authenticate!() 
end 

puts "Succesfully called authenticate!" 
#outside of catch(:) guard 
authenticate!() 
puts "this never gets executed" 

如果我執行這個它會做:

ruby exc.rb 
Calling authenticate! 
Succesfully called authenticate! 
exc.rb:2:in `throw': uncaught throw :warden (ArgumentError) 
    from exc.rb:2:in `initialize!' 
    from exc.rb:12:in `<main>' 

正如你可以看到,第二次我打電話給身份驗證!我在捕獲區(:warden)區塊之外,因此當我投擲時:監獄長沒有捕捉區塊來捕捉它,例外發生。

這就是發生在你身上,看看warden#authenticate!

def authenticate!(*args) 
    user, opts = _perform_authentication(*args) 
    throw(:warden, opts) unless user 
    user 
end 

見擲(:區長,選擇採用)?如果該投擲在catch(:warden)塊之外,則引發異常。守望者應該在守衛區守衛你的整個應用程序,這樣你就可以在任何時候拋出:守望者。但由於某種原因,這在zeepauto上不會發生。

你的問題是,監獄長沒有正確設置(沒有config/initializers/warden.rb)和call (env),所以你的catch(:warden)後衛從未設置。

你的答案就在這裏:https://github.com/hassox/warden/wiki/Setup

只是通過建立自己的工作。你可以在任何時候投擲:監獄長來測試你的開發環境。只要寫像一個測試:

it "warden should catch the throw :warden at any point" do 
    throw(:warden) 
end 

如果你想獲得這個東西快,剛剛獲得railscasts.com親帳戶並觀看:http://railscasts.com/episodes/305-authentication-with-warden這一集將引導您完成安裝。

相關問題