2015-06-10 50 views
3

我要保存的電話號碼型號的用戶,但軌意外轉換手機從字符串到整數類型軌強PARAMS意想不到的字符串轉換爲整數

def create 
    @user = User.new(user_params) 
    if @user.save 
     log_in @user 
     user_response = { 
      'user_id' => @user.id 
     } 
     render json: user_response 
    else 
     render json: @user.errors, status: :unprocessable_entity 
    end 
end 

private 
def user_params 
    params.require(:user).permit(:username, :phone, :password, :password_confirmation) 
end 

錯誤:

Parameters: {"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "phone"=>"12345678901", "username"=>"test", "user"=>{"username"=>"lv", "phone"=>"12345678901"}} 

    (0.1ms) BEGIN 

    User Exists (0.2ms) SELECT 1 AS one FROM `users` WHERE `users`.`phone` = **BINARY '18610012942'** LIMIT 1 

    (0.1ms) ROLLBACK 

Completed 422 Unprocessable Entity in 6ms (Views: 0.2ms | ActiveRecord: 0.5ms) 

誰能幫助我? 非常感謝。

UPDATE:

遷移:

class AddPhoneToUsers < ActiveRecord::Migration 
    def change 
    add_column :users, :phone, :string, index: true 
    end 
end 

型號:

class User < ActiveRecord::Base 
attr_accessor :remember_token 

validates :phone, presence: true, length: {minimum: 11, maximum: 11}, 
         uniqueness: true, format: {with: VALID_PHONE_REGEX} 

end 
+0

顯示你'users'遷移'table' – Prashant4224

+0

顯示爲'users'表,請您模型代碼請在form變化! –

+0

感謝您的回覆,已經更新 –

回答

0

你的控制檯輸出表示

{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "phone"=>"12345678901", "username"=>"test", "user"=>{"username"=>"lv", "phone"=>"12345678901"}} , 

相反,它應該是這樣的:

{"user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "phone"=>"12345678901", "username"=>"test"}} 

根據上述