开发者

Rails 3 AJAX: wrong constant name

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


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.


You are missing the return in

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

Does that make a difference?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜