rails + backbone报告模型中的确认字段在创建时为空

时间:2013-03-14 18:09:15

标签: ruby-on-rails validation backbone.js model

我通过骨干网创建用户模型。密码字段有一个确认字段。如果我通过控制台创建用户 - 它已创建。但如果我尝试通过主干创建模型,那就是错误 - "密码确认不能为空白"。
我已经找到了拼写错误或任何其他简单的错误......这是我的代码 视图:

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

更新1
如果我将:password_confirmation插入attr_accessible我会收到500错误作为回复,但会创建新用户!我不认为压制这个错误是正确的想法,所以希望找到更好的方法......

更新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_confirmationuser内不存在......我不知道为什么 - 看一下骨干视图......它就在那里^ ( 还有一个模型 -

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

1 个答案:

答案 0 :(得分:0)

最后,我在控制器

中添加了location属性
  def create
    respond_with User.create(params[:user]),:location => signup_path
  end

并在模型中将:password_confirmation添加到attr_accessible。就是这样!