我正在通过主干创建用户模型。密码字段有一个确认字段。如果我通过控制台创建用户 - 它已创建。但如果我尝试通过主干创建模型,则会出现错误 - “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
更新 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_confirmation
里面不存在user
......我不知道为什么 - 看看主干视图......它都在那里 ^(还有一个模型 -
class Notes.Models.User extends Backbone.Model
url: '/api/account'
# paramRoot: 'user' i tried with or without this line... Still error with