422错误,设计/主干

时间:2013-02-09 00:48:35

标签: backbone.js devise

我正在尝试设置一个使用Backbone和Devise进行注册的Rails应用程序。

Chrome控制台错误回调中的响应文字显示

responseText: "{"errors":{"email":["can't be blank"],"password":["can't be blank"]}}"

但是,服务器中的日志表示不可处理的实体

 Parameters: {"email"=>"pp@rr.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "registration"=>{"email"=>"pp@rr.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 422 Unprocessable Entity in 4ms (Views: 0.3ms | ActiveRecord: 0.1ms)

我有一个Backbone用户模型,用于设置保存的URL

UserRegistration = Backbone.Model.extend({
          url: '/users.json',
          paramRoot: 'user',

          defaults: {
            "email": "",
            "password": "",
            "password_confirmation": ""
          }
    });

在关联视图中,我从注册表单中获取属性,在模型中设置它们,然后保存模型

     var email = $('#email').val();
    var password_confirmation = $('#password_confirmation').val();
    var password = $('#password').val();

    this.model.set({email : email, password_confirmation: password_confirmation, password: password})



    this.model.save(this.model.attributes, {
      success: function(userSession, response) {
        console.log("success");
        console.log(userSession);
        console.log(response);
        console.log(this.model.url);

      },
      error: function(userSession, response) {
        console.log("error");
        console.log(userSession);
        console.log(response);

      }
    });
  }

设置模型属性后(保存前)我做了一个console.log(this.model.attributes),然后设置了

Object {email: "oo@gmail.com", password: "snowy", password_confirmation: "snowy"} 

我的用户模型看起来像这样

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

end

有人可以提出任何建议吗?

最近有一些Devise版本的问题只响应了html,所以我安装了Devise 2.1.2以使其响应json以使其与Backbone兼容。这不是问题所在。

1 个答案:

答案 0 :(得分:1)

paramRoot不是Backbone核心的一部分。为了解决这个问题,我必须在Backbone-Rails gem中包含同步库https://raw.github.com/codebrew/backbone-rails/master/vendor/assets/javascripts/backbone_rails_sync.js,以使用户成为param root的一部分

  url: '/users.json',
  paramRoot: 'user',