我用我的用戶模型,我理解創建兩個虛擬域的方法has_secure_password不工作,但形式
has_secure_password
什麼時候:
Password
Password_confirmation
並驗證precense他們的,所以在我身邊我做這些人的驗證:
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
因此當用戶submmit的用戶獲取與現場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:"[email protected]",
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)"]}}
'password'和'password_confirmation'具有單引號而不是double。這很重要嗎? –
@GraemeMcLean我不這麼認爲,只是我嘗試了一些東西......而且沒有成功。 –