0

在带有(Gemfile)的应用程序中:

gem "devise", ">= 2.1.2"
gem 'rails', '3.2.8' 
gem 'jquery_mobile_rails'

在添加 jquery-mobile 之前,一切正常,当输入无效数据时,我得到带有错误消息和日志的页面:

Started POST "/users/sign_in" for 127.0.0.1 at 2012-11-28 12:01:13 -0500
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jLtHS2q0wO+tgswm9VM2/EiuTiq7J9Wwp8d65t/RSGE=", "user"=>{"email"=>"", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
Completed 401 Unauthorized in 0ms
Processing by Devise::SessionsController#new as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jLtHS2q0wO+tgswm9VM2/EiuTiq7J9Wwp8d65t/RSGE=", "user"=>{"email"=>"", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
  Rendered devise/shared/_links.html.erb (0.8ms)
  Rendered devise/sessions/new.html.erb within layouts/application (3.2ms)
  Rendered layouts/_navigation.html.erb (0.5ms)
  Rendered layouts/_messages.html.erb (0.0ms)
Completed 200 OK in 29ms (Views: 28.9ms)

添加 jquery-mobile 后,使用正确数据登录时工作正常,但使用无效数据我得到 401 并且不重定向注意。在 http 响应中(使用 firebug 查看)仅显​​示带有“无效电子邮件”(不是 html 代码或 json)之类的错误消息,并且 jquery-mobile 显示“错误加载页面”。

Started POST "/users/sign_in" for 127.0.0.1 at 2012-11-27 21:32:45 -0500
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"EYx56pui4v6trcuzmXQuhzY2BDHwqrADepKuySx9ub0=", "user"=>{"email"=>"test@test.net", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
Completed 401 Unauthorized in 0ms

我生成了默认设计控制器和默认视图。

如何使用 jquery mobile 处理设计的错误消息?

4

2 回答 2

1

仅不要将 ajax 与 jquery-mobile 一起使用并设计为默认值。

config/initializers/devise.rb

# If http headers should be returned for AJAX requests. True by default.
  config.http_authenticatable_on_xhr = false
于 2012-12-07T01:36:32.287 回答
0

另一种解决方案是添加

:html => {:data => {:ajax => 'false'}}

到有问题的表单,所以 jQM 不会劫持它并将其转换为 ajax 请求。

请查看我对同一问题的完整答案https://stackoverflow.com/a/14595607/64669

于 2013-01-30T02:21:39.950 回答