3

我正在尝试在我当前的 rails 应用程序上设置基于 json 的身份验证。该应用程序的身份验证当前由设计处理。

我已经阅读了几个关于 stackoverflow 的问题,但我似乎没有让它工作。

路线.rb:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :sessions => 'users/sessions' }

会话控制器:

class Users:: SessionsController < Devise::SessionsController
  def create
    resource = warden.authenticate!(:scope => resource_name, :recall => :failure)
    return sign_in_and_redirect(resource_name, resource)
  end

  def sign_in_and_redirect(resource_or_scope, resource=nil)
    scope = Devise::Mapping.find_scope!(resource_or_scope)
    resource ||= resource_or_scope
    sign_in(scope, resource) unless warden.user(scope) == resource
    return render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)}
  end

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

不知道我哪里出错了。我正在打的电话是:

JSON:

Content-Type: application/json
Accept: application/json
data: {user: {password: mypass, email: some_email@gmail.com}}

错误:

MultiJson::DecodeError (756: unexpected token at '{user: {password: mypass, email:    some_email@gmail.com}}'):
2012-06-30T18:10:10+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/json-1.6.5/lib/json/common.rb:148:in `parse'
4

1 回答 1

1

如果您将该 JSON 粘贴到http://jsonlint.com中,您会得到相同的错误。相反,您应该将值用引号括起来:

{
    "user": {
        "password": "mypass",
        "email": "some_email@gmail.com"
    }
}
于 2012-06-30T18:45:34.250 回答