Rails 3 AJAX:常量名称错误

发布于 2024-12-03 18:32:49 字数 673 浏览 1 评论 0原文

我正在尝试使用 Devise 进行 Ajax 登录,如下所述: http://jessehowarth.com/2011/04/27/ajax-login-with-devise#comment-5(请参阅 jBeasley 的评论)。

我的控制器正在尝试返回,

class Users::SessionsController < Devise::SessionsController
  def failure
    render :json => {:success => false, :errors => ["Login failed."]}
  end
end

这会导致此错误:

NameError (wrong constant name ["{\"success\":false,\"errors\":[\"Login failed.\"]}"]Controller):

并且 Firebug 显示 [500 内部服务器错误]。

我该如何解决这个问题?我正在运行 Rails 3.1 并设计 1.4.5。

谢谢!!

I am trying to do Ajax login with Devise, as explained here: http://jessehowarth.com/2011/04/27/ajax-login-with-devise#comment-5 (see comment from jBeasley).

My controller is attempting to return

class Users::SessionsController < Devise::SessionsController
  def failure
    render :json => {:success => false, :errors => ["Login failed."]}
  end
end

which results in this error:

NameError (wrong constant name ["{\"success\":false,\"errors\":[\"Login failed.\"]}"]Controller):

and Firebug showing [500 Internal Server Error].

How can I fix this? I am running Rails 3.1 and devise 1.4.5.

Thanks!!

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

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

发布评论

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

评论(2

想你的星星会说话 2024-12-10 18:32:49

您是否执行了 Jeff Poulton 在评论 #4 中推荐的步骤? 1.4.5 中的 :recall 选项看起来与旧版本完全不兼容。现在,它要求您发送控制器,而在本教程中,您遵循的只是发送操作(旧方式)。

在你的情况下, :recall => :failure 必须更改为 :recall => Devise 1.4.5 中的“users/sessions#failure”

这是因为确定故障操作的控制器的方式所致。在旧版本中,它只是从参数中提取。

def recall_controller
  "#{params[:controller]}.camelize}Controller".constantize
end

# called via recall_controller.action(warden_options[:recall]).call(env)

在 1.4.5 中,它需要一个以路由样式指定控制器和操作的字符串:

def recall_app(app)
  controller, action = app.split('#')
  controller_name = ActiveSupport::Inflector.camelize(controller)
  controlller_klass = ActiveSupport::Inflector.constantize("#{controller_name}Controller")
  controller_klass.action(action)
end

# called via recall_app(warden_options[:recall]).call(env)

看起来您的应用实际上是将选项的 JSON 化哈希传递给 recall_app,其中缺少'#' 不会被拆分,整个字符串会连接到“Controller”以尝试确定故障控制器的类。

Did you do the step recommended by Jeff Poulton in comment #4? The :recall option in 1.4.5 looks to be completely incompatible to older versions. It now requires you send the controller, whereas in the tutorial you're following he just sends the action (the old way).

In your case, :recall => :failure must be changed to :recall => "users/sessions#failure" in Devise 1.4.5.

This is because of the way the controller for the failure action is determined. In older versions, it was simply pulled from the params.

def recall_controller
  "#{params[:controller]}.camelize}Controller".constantize
end

# called via recall_controller.action(warden_options[:recall]).call(env)

In 1.4.5, it expects a string specifying the controller and action, in the style of routes:

def recall_app(app)
  controller, action = app.split('#')
  controller_name = ActiveSupport::Inflector.camelize(controller)
  controlller_klass = ActiveSupport::Inflector.constantize("#{controller_name}Controller")
  controller_klass.action(action)
end

# called via recall_app(warden_options[:recall]).call(env)

It would seem as though your app is actually passing the JSONified hash of options to recall_app, which, lacking a '#', isn't being split, and the entire string is concatenated to "Controller" to attempt to ascertain the failure controller's class.

落墨 2024-12-10 18:32:49

中的退货。

def failure
    return render:json => {:success => false, :errors => ["Login failed."]}
end

您错过了“这有什么影响吗?”

You are missing the return in

def failure
    return render:json => {:success => false, :errors => ["Login failed."]}
end

Does that make a difference?

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