0

백본 1.1.2와 레일즈 4.2.beta1을 사용하여 사용자 계정을 만들려고합니다. 백본에서레일스 account_params가 백본에서 매개 변수를 잃어 버림`.save()`

나는 호출 오전 : 올바르게 다음과 같은 요청 매개 변수를 사용하여 내 웹 서비스에 레일을 create 메소드를 호출

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}); 
} 

:

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

그것은 필터 방법을 겪고있다

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

하지만 위의 중단 점을 배치하면 나에게 첫눈에 올바른 보이는

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

: 에드 자리는 내가 얻을 params 개체를 검사합니다.

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

왜 계정 매개 변수 객체에 포함되지 않는 암호입니다 : 만 얻을 account_params의 결과를 검사하는 경우 암호가 nil

때문에 그러나 계정 생성이 실패합니다. 레일과 백본의 기본 구성으로 모든 기본 고정 코드를 사용하고 있습니다.

+0

양식을 게시 할 수 있습니까? – Mandeep

+0

@Mandeep 위의 코드를 편집했습니다. 그것은 형식이지만'$(). val()'을 사용하여 양식을 읽고 있으므로이 코드를 포함 시켰습니다. – SnareChops

+0

아, 그리고 어떻게이 매개 변수를 서버에 보내고 있습니까? 귀하의 params 해시를 살펴보면 그들은 올바르게 중첩되지 않는 것 같습니다. – Mandeep

답변

0

루비 보석 backbone-rails에 대해 GitHub 레포를 찾고 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); 
} 

은 내가 필요하지 않은 일부 항목을 삭제, 그러나 이것은 백본 객체에 속성과 필요로하는 형식으로 레일 응용 프로그램에 전송 랩합니다. 그래서

매개 변수 :

{"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 개체를 확장하여 다른 .d.ts 파일에 추가하십시오. 그러나 이것은 나중에 프로젝트에서 이것을 사용할 계획이므로 나는 괜찮습니다. .d.ts 파일을 약간 수정하는 데 신경 쓰지 않아도됩니다.

앞으로 도움이되기를 바랍니다.

관련 문제