2014-05-09 106 views
1

我用我的用戶模型,我理解創建兩個虛擬域的方法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)"]}} 
+0

'password'和'password_confirmation'具有單引號而不是double。這很重要嗎? –

+0

@GraemeMcLean我不這麼認爲,只是我嘗試了一些東西......而且沒有成功。 –

回答

2

貌似這個問題已經因爲您的JSON參數不是根節點下(你可能有參數包裹在配置/初始化/ wrap_parameters.rb開啓)與參數結束語http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html

做,領域會自動被包裝在與控制器同名的根(user)中。由於password是一個虛擬的屬性,它不裹不受attribute_names返回:

在ActiveRecord的模型沒有:包括或:排除選項設置,它 只能換用類方法返回的參數 attribute_names 。

我認爲這裏的正確的解決方法是在wrap_parametersinclude列表中添加此向您UsersController

wrap_parameters include: User.attribute_names + [:password] 

頂部指定password或作出與user根節點

的JSON請求
var ad = { 
      user: 
      { 
      name:"juan", 
      last_name:"paco", 
      cellphone:"1234567890", 
      phone:"1234567890", 
      address:"porche", 
      password:"porche", 
      password_confirmation:"porche", 
      email:"[email protected]", 
      city_id: 1 
      } 
     }; 
+0

謝謝,但我沒有得到與用戶根節點的JSON請求的第二個選項,我看到我發送相同的JSON ... –

0

我已經找到了解決辦法,也許沒有TE最實用的,但它確實工作...

I'va改變了創建方法:

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 

,因爲我被檢查軌道服務器日誌和我看到了一些差異在html請求的參數和JSON請求上的params。

當有人submmit一個形式,我得到這樣的:

Started POST "https://stackoverflow.com/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"=>"[email protected]", "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"=>"[email protected]", "city_id"=>1}} 

我認爲這是對的form_for至極創建的一個實例用戶對象雖然de JSON並不能告訴我爲什麼這個修復它?