Reils strong options to unexpectedly convert string to integer

I want to store the user of the model with a phone number, but rails are unexpectedly converting phone from a string to an integer type

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

      

Mistake:

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)

      

Can anyone help me? Many thanks.

UPDATE:

migration:

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

      

Model:

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

      

+3


source to share


1 answer


Your console output showing

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

      

Instead, it should look like this:



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

      

Modify as form

above

0


source







All Articles