gpt4 book ai didi

ruby-on-rails - rails 3 管理员 NameError (uncaught throw `warden' ) :

转载 作者:行者123 更新时间:2023-12-03 17:34:55 24 4
gpt4 key购买 nike

这是它应该如何工作:
我登录到管理面板,转到汽车/新车并填写字段,按创建,我的列表中应该有一辆新车。 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

用户 Controller : http://pastie.org/3028586

我可以编辑汽车,它工作正常,所以来自cars_controller 的更新和编辑功能没问题,我检查了来自cars_controller 的新功能和创建功能,但我无法确定任何能让我了解发生了什么的事情。汽车 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”的进程正在日志中运行以更新汽车模型列表,此时 session key 被另一个更改,通常如果 session key 更改,我登录时启动的 session 不再有效,这就是我收到“未经身份验证的错误”的原因。仍然不知道究竟是什么 jquery 导致了问题,但最终我解决了这个问题,这不是因为管理员配置。

最佳答案

好的,我会向您解释为什么会非常小心地发生此异常,但我无法为您修复。

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) 了吗?如果抛出在 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那一集将指导您完成设置。

关于ruby-on-rails - rails 3 管理员 NameError (uncaught throw `warden' ) :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8540695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com