0

バックボーンを介してユーザーモデルを作成しています。パスワードフィールドには確認フィールドがあります。コンソールを介してユーザーを作成すると、作成されます。しかし、バックボーンを介してモデルを作成しようとすると、エラーになります-「Password_confirmationを空白にすることはできません」。
私はかなりのワイルのためにタイプミスや他の単純なエラーを探しました...これが私のコードビューです:

class Notes.Views.signupView extends Backbone.View
  template: JST['users/signup']
  events:
    'submit form#create_user': 'createUser'
  checkField: (fieldId) ->
    $('#'+fieldId).val() != ''

  inputFilled: ->
    @checkField( 'name' ) && @checkField( 'email' ) && @checkField( 'password' ) && $('#password').val() == $('#password_confirmation').val()
  readAttributes: ->
    atr =
      name: $('#name').val()
      email: $('#email').val()
      password: $('#password').val()
      password_confirmation: $('#password_confirmation').val()

  createUser: (e)->
    e.preventDefault()
    unless @inputFilled()
      alert 'All fields must be filled, passwords must match'
      return
    attributes = @readAttributes()
    @model = new Notes.Models.User(attributes)
    @model.save attributes,
      wait: true
      success: -> alert('OK')
      error: @handleError
  handleError: (entry,response) ->
    if response.status == 422
      errors = $.parseJSON(response.responseText).errors
      for attribute, messages of errors
        alert "#{attribute} #{message} " for message in messages
  render: ->
    $(@el).html(@template())
    this

コンソールは、リクエストが次のパラメータで送信されていることを示しています。

{"name":"elmor","email":"elmorelmor@ukr.net","password":"elmor","password_confirmation":"elmor"}

それへの応答

{"errors":{"password_confirmation":["can't be blank"]}}

そして私のモデル:

class User < ActiveRecord::Base
  attr_accessor :password
  attr_protected :password_digest
  attr_accessible :password, :facebook, :linkedin, :name, :email,  :twitter, :web
  validates :name, presence: true, uniqueness: true
  validates :email, presence: true, uniqueness: true, email: true
  validates :password, presence: true, :confirmation => true
  validates :password_confirmation, presence: { if: :password }

  def password=(pass)
    return if pass.blank?
    @password = pass
    self.password_digest = BCrypt::Password.create(pass)
  end
end


挿入:password_confirmationするattr_accessibleと応答として500エラーが発生しますが、新しいユーザーが作成された場合は1を更新してください。このエラーを抑えることが正しい考えだとは思わないので、より良い方法を見つけたいと思っています...

アップデート2
コントローラーのコードを追加するのを忘れた

class UsersController < ApplicationController
  respond_to :json
  def create
    respond_with User.create(params[:user])
  end

end

アップデート3
このエラーの原因を見つけたと思います-production.logで、このフォームを送信するとエラーが表示されます

Processing by UsersController#create as JSON

      Parameters: {"name"=>"elmor", "email"=>"elmor@ukr.net", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "user"=>{"password"=>"[FILTERED]", "name"=>"elmor", "email"=>"elmor@ukr.net"}}

password_confirmation 内部には存在しませんuser...そして理由はわかりません-バックボーンビューを見てください...それはすべてそこにあります^(そしてモデル-

class Notes.Models.User extends Backbone.Model
  url: '/api/account'
  # paramRoot: 'user' i tried with or without this line... Still error with
4

1 に答える 1

0

最後に、コントローラーのアクションにロケーション属性を追加しました

  def create
    respond_with User.create(params[:user]),:location => signup_path
  end

モデルに追加:password_confirmationattr_accessibleれます。以上です!

于 2013-03-16T11:49:38.253 に答える