Rails 3 Warden NameError(未捕获抛出“warden”):

发布于 2024-12-21 18:26:58 字数 2157 浏览 0 评论 0原文

它应该是这样工作的: 我登录到管理面板,转到“汽车/新车”并填写字段,按“创建”,我的列表中应该有一辆新车。 www.autozeep.com

事情是一切正常,直到我按下“创建”按钮来创建新车,服务器日志显示这:

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 没问题,我检查了 cars_controller 中的新函数和创建函数,但我无法找到任何能让我了解正在发生的事情的东西。 Cars_controller:http://pastie.org/3028452

请帮忙,我已经运行了这个应用程序,客户端正在等待为了解决这个问题。非常感谢大家。

编辑

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”的进程来更新汽车模型列表,此时会话密钥也正在运行被另一个更改,通常如果会话密钥被更改,我登录时启动的会话将不再有效,这就是为什么我收到“未经身份验证的错误”的原因。仍然不知道jquery到底是什么导致了这个问题,但最后我解决了这个问题,这不是因为典狱长配置。

this is how it should work:
I log in to the admin panel, go to cars/new and fill up the fields, press create and I should have a new car in my list. www.autozeep.com

the thing is that it goes ok until I press the Create button to create the new car, server logs shows this:

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

in the development mode this is working fine, on the server in production mode it's not, it's the same code, nothing changed.
for more server logs: 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

I can edit a car, it works fine so the update and edit functions from cars_controller are ok, I checked the new and create functions from cars_controller but I couldn't fine anything that would give me an idea of what is going on. Cars_controller: http://pastie.org/3028452

Please help, I have this app allready running and the client si waiting for this problem only to be solved. Thank you all very much.

EDIT

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"}

I get this errors only when I add new car, I can edit cars, news, contacts. everything except cars.

PROBLEM SOLVED

This problem was caused by some jquery library, I am using dynamic_form in this form so when I select car name in the next select_box appears only models for the selected car. Inspecting the problem (with my teacher, I wouldn't think of it by myself) we see that when I select car, a process called "dynamic_carmodels" is running in the logs to update the carmodels list and also at this point the session key is changed by another one, normally if session key is changed, the session that I started when I logged in is not valid anymore and that's why I get an "unauthenticated error". Still have no idea what jquery exactly was causing the problem but finally I got this solved, this was not because of the warden configuration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2024-12-28 18:26:58

好吧,我会非常仔细地向您解释为什么会发生此异常,但我无法为您修复它。

Warden 使用 catch(:warden) 块保护您的应用程序,您可以在以下位置看到这一点:

# 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) 中被调用,如果您的应用程序 throws(:warden) 它就会被捕获。这就是 throw、catch 的工作原理,这是一个示例:

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>'

正如您所看到的,我第二次调用验证!我在 catch(:warden) 块之外,因此当我抛出 :warden 时,没有 catch 块来捕获它,发生了异常。

这就是发生在你身上的事情,看看 warden#authenticate!

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

看到 throw(:warden, opts) 了吗?如果该 throw 位于 catch(:warden) 块之外,则会引发异常。 Warden 应该在 catch 块中保护你的整个应用程序,这样你就可以在任何时候抛出 :warden 。但由于某种原因,这在 zeepauto 上并没有发生。

你的问题是典狱长没有正确设置(没有config/initializers/warden.rb)和call (env)所以你的catch(:warden)守卫永远不会放。

您的答案就在这里: https://github.com/hassox/warden/wiki/Setup

自行解决设置。您可以随时通过抛出 :warden 来测试您的开发环境。只需编写一个测试,例如:

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

如果您想更快地获得此功能,只需在railscasts.com 中获得一个专业帐户并观看:http://railscasts.com/episodes/305-authentication-with-warden 该集将引导您完成设置。

Ok I will explain to you why this exception is happening very carefully but I can't fix it for you.

Warden guards your application with a catch(:warden) block, you can see this in:

# 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

Your application gets called in @app.call(env) and if your application throws(:warden) it gets caught. this is how throw, catch works, here is an example:

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"

If I execute this it will do:

 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>'

As you can see, the 2nd time I called authenticate! I was outside the catch(:warden) block, therefore when I throw :warden there was no catch block to catch it, exception ocurred.

This is what is happening to you, look at warden#authenticate!:

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

See the throw(:warden, opts) ? If that throw is outside the catch(:warden) block the exception is raised. Warden is supposed to guard your whole app in the catch block, so that you can throw :warden at any point. But for some reason this is not happening on zeepauto.

Your problem is that warden is not properly setup(there is no config/initializers/warden.rb) and call (env)so your catch(:warden) guard is never set.

Your answer is here: https://github.com/hassox/warden/wiki/Setup

Just work yourself through the setup. You can test on your dev environment by throwing a :warden at any time. Just write a test like:

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

If you want to get this thing faster, just get a pro account in railscasts.com and watch: http://railscasts.com/episodes/305-authentication-with-warden that episode will guide you through the setup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文