0

我想创建一个用户帐户使用骨干1.1.2和Rails 4.2.beta1。Rails的account_params缺少骨干参数`.save()`

骨干,我呼吁:

public create(){ 
    var email:string = $('#create-account-email').val(), password:string = $('#create-account-password').val(), passconf:string = $('#create-account-password-confirm').val(); 
    this.model.set({email: email, password: password, password_confirmation: passconf}); 
    this.model.save(null, {success: this.success, error: this.error}); 
} 

其正确调用轨create方法在我的web服务与下面的请求参数:

{"email":"[email protected]","password":"123456","password_confirmation":"123456"} 

它是通过过滤方法去

def account_params 
    #breakpoint 
    params.require(:account).permit(:password, :password_confirmation, :email) 
end 

但如果我在上面没有放置断点编辑点和检查params对象,我得到:

{"email"=>"[email protected]", 
"password"=>"123456", 
"password_confirmation"=>"123456", 
"controller"=>"accounts", 
"action"=>"create", 
"account"=>{"email"=>"[email protected]"}, 
"format"=>"json"} 

这对我来说看起来是正确的第一眼。

{"email" => "[email protected]") 

为什么没有被列入考虑的参数对象的密码:但是,因为密码是nil

如果检查的account_params结果我只得到账号创建失败。我正在使用所有带有rails的默认sacaffolded代码和Backbone的默认配置。

+0

你可以发表你的表格吗? – Mandeep 2014-10-05 18:01:37

+0

@Mandeep我为你编辑了上面的代码。它是一种形式,但是我正在使用'$()。val()'将它从表单中读出来,所以我将这些代码包含在内。 – SnareChops 2014-10-05 18:04:27

+0

啊好的,你如何将这些参数发送到服务器?看着你的参数哈希他们似乎并没有正确嵌套 – Mandeep 2014-10-05 18:14:12

回答

0

我发现一个GitHub红宝石宝石backbone-rails回购,并使用他们包括here同步覆盖。

Backbone._sync = Backbone.sync; 
Backbone.sync = function (method:string, model:Backbone.Model, options?: any){ 
    if(options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) { 
     options.contentType = 'application/json'; 
     var data:any = JSON.stringify(options.attrs || model.toJSON(options)); 
     if(model.paramRoot) { 
      data = {}; 
      data[model.paramRoot] = model.toJSON(options); 
     } else { 
      data = model.toJSON(); 
     } 
     options.data = JSON.stringify(data); 
    } 
    return Backbone._sync(method, model, options); 
} 

我删除了一些项目,我并不需要,但这种包装的骨干属性引入到对象,并将其发送到它所需要的格式Rails应用程序。

所以参数:

{"email":"[email protected]", 
"password":"123456", 
"password_confirmation":"123456"} 

成为:

{"account": 
    {"email":"[email protected]", 
    "password":"123456", 
    "password_confirmation":"123456"} 
} 

这不是没有返工的主干应用程序代码的一点点...

由于我使用的打字稿我必须使用以下签名将_sync()方法添加到backbone.d.ts文件。

declare module Backbone { 

    function _sync(method: string, model: Model, options?: JQueryAjaxSettings):any; 

    ... 
} 

我还需要以下添加到Backbone.Model

class Model extends ModelBase implements OptionalDefaults { 

    paramRoot: string; 

    ... 
} 

然后我加入了paramRoot属性,我的模型:

export class Account extends Backbone.Model { 
    public urlRoot: string; 
    public validation:any; 
    public paramRoot:string; 
    constructor(attributes?: any, options?: any){ 
     this.paramRoot = 'account'; 
     this.urlRoot = 'http://mydomain.fake/accounts'; 
     this.validation = { 
      email: { 
       required: true, 
       pattern: 'email' 
      }, 
      password: { 
       required: true, 
       minLength: 6 
      } 
     }; 
     super(attributes, options); 
    } 
} 

还有其他的方法,使_sync()方法和paramRoot属性传递编译器错误而不修改backbone.d.ts文件,但inst ead扩展对象并将其添加到另一个.d.ts文件中。但是这对我来说是好的,因为我打算在未来的项目中使用它,我不介意.d.ts文件被稍微修改。

希望这可以帮助未来的人。