我正在使用方法
has_secure_password
在我理解的用户模型上创建了两个虚拟字段:
Password
Password_confirmation
并验证它们的前置,所以在我身边,我正在做这些其他验证:
validates :password , length: {minimum: 6}
validates :password_confirmation , length: {minimum: 6}
在我看来,我有这样的形式:
<%= form_for :user, url:{controller: 'users', action: 'create'}, html:{class:"form-horizontal"} do |f| %>
<div class="form-group">
<%= f.label(:name, "Nombre", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.text_field(:name, class:"form-control") %>
</div>
</div>
<div class="form-group ">
<%= f.label(:last_name, "Apellido", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.text_field(:last_name, class:"form-control ") %>
</div>
</div>
<div class="form-group ">
<%= f.label(:cellphone, "Celular", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.text_field(:cellphone, class:"form-control") %>
</div>
</div>
<div class="form-group ">
<%= f.label(:phone, "Telefono", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.text_field(:phone, class:"form-control") %>
</div>
</div>
<div class="form-group ">
<%= f.label(:address, "Direccion", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.text_field(:address, class:"form-control") %>
</div>
</div>
<div class="form-group ">
<%= f.label(:email, "Correo", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.text_field(:email, class:"form-control") %>
</div>
</div>
<div class="form-group ">
<%= f.label(:password, "Contraseña", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.password_field(:password, class:"form-control") %>
</div>
</div>
<div class="form-group ">
<%= f.label(:password_confirmation, "Confirmar Contraseña", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.password_field(:password_confirmation, class:"form-control") %>
</div>
</div>
<div class="form-group ">
<%= f.label(:city_id, "Ciudad", class:"control-label col-sm-1") %>
<div class="col-sm-3">
<%= f.select(:city_id, @cities.map {|c| [c.name, c.id]}) %>
</div>
</div>
<div class="form-buttons">
<%= submit_tag("Create Section") %>
</div>
<% end %>
并且控制器具有以下操作:
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to controller: "users", action: "show"
else
@cities = City.all
render 'new'
end
end
private
def user_params
params.require(:user).permit(:name, :last_name, :email, :address, :password, :password_confirmation, :cellphone, :city_id, :phone)
end
因此,当用户提交表单时,用字段password_digest下的加密密码创建用户,并且它可以正常工作。
我的实际问题是它的控制器让客户端通过发送json创建一个新用户,这是api控制器的方法:
respond_to :json
def create
respond_with User.create(user_params)
end
private
def user_params
params.require(:user).permit(:name, :last_name, :email, :address, :password, :password_confirmation, :cellphone, :city_id, :phone)
end
当我发送这个JSON时:
var ad = {
name:"juan",
last_name:"paco",
cellphone:"1234567890",
phone:"1234567890",
address:"porche",
password:"porche",
password_confirmation:"porche",
email:"kkk@hj.com",
city_id: 1,
};
到
localhost.com:3000/api/v1/users/create
我得到:
{"errors":{"password":["can't be blank","is too short (minimum is 6 characters)"],"password_confirmation":["is too short (minimum is 6 characters)"]}}
答案 0 :(得分:2)
看起来这个问题与参数包装http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html
有关由于您的json参数不在根节点下(并且您可能在config / initializers / wrap_parameters.rb中打开了参数包装),因此这些字段将自动包装在与控制器同名的根目录中({ {1}})。由于user
是一个虚拟属性,password
不会返回它,因此它不会被包装:
在没有:include或:exclude选项设置的ActiveRecord模型上,它 只会包装类方法返回的参数 attribute_names。
我认为这里正确的解决方案是在attribute_names
的{{1}}列表中指定password
,将其添加到include
的顶部
wrap_parameters
或使用UsersController
根节点
wrap_parameters include: User.attribute_names + [:password]
答案 1 :(得分:0)
我找到了一个解决方案,也许没有最实用的解决方案,但是它确实有效......
我将创建方法更改为:
def create
new_parms = user_params
new_parms[:password] = params[:password]
new_parms[:password_confirmation] = params[:password_confirmation]
respond_with User.create(new_parms)
end
因为我正在检查rails服务器日志,我在html请求的params和JSON请求上的params中看到了一些不同之处。
当有人提交表格时我得到了这个:
Started POST "/users/create" for 127.0.0.1 at 2014-05-09 16:56:07 -0400
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5HZn9RRbdNAzSirAb9FauNYj/Jr/AAkKBecTvzrih/8=", "user"=>{"name"=>"", "last_name"=>"", "cellphone"=>"", "phone"=>"", "address"=>"", "email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "city_id"=>"1"}, "commit"=>"Create Section"}
当我发送JSON请求时,我得到了这个:
Started GET "/api/v1/users/creando" for 127.0.0.1 at 2014-05-09 16:58:34 -0400
Processing by API::V1::UsersController#creando as JSON
Parameters: {"name"=>"juan", "last_name"=>"paco", "cellphone"=>"1234567890", "phone"=>"1234567890", "address"=>"porche", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "email"=>"kkk@hj.com", "city_id"=>1, "commit"=>"Create Section", "authenticity_token"=>"5HZn9RRbdNAzSirAb9FauNYj/Jr/AAkKBecTvzrih/8=", "user"=>{"name"=>"juan", "last_name"=>"paco", "cellphone"=>"1234567890", "phone"=>"1234567890", "address"=>"porche", "email"=>"kkk@hj.com", "city_id"=>1}}
我认为这是为form_for创建User对象的实例而de JSON没有人可以告诉我为什么要修复它?