【问题标题】:422 error, devise/backbone422 错误,设计/主干
【发布时间】:2013-01-24 21:00:37
【问题描述】:

我正在尝试设置一个使用 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 兼容。这不是这里的问题。

【问题讨论】:

  • 你在使用backbone-rails吗?
  • @muistooshort 骨干轨上的宝石。
  • paramRoot: 'user' 应该在与服务器通信时将所有数据放在'user' 命名空间中,不是吗?但请注意,params 中的所有内容都处于顶层...
  • @muistooshort 我正在使用的设计相关代码的一部分(包括 paramRoot)取自可能一直在使用 Backbone-Rails 的人。我认为 paramRoot 来自那个宝石,而不是 Backbone 核心的一部分。这可能是问题所在。你知道如何在不使用该 gem 的情况下添加类似于 paramRoot 的内容吗?

标签: backbone.js devise


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 2012-10-15
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多