Adding a phone number to a user model

Apologizes if this question is too general. I am relatively new to Rails and the development in general. I am creating a Rails application that uses Twilio verification for SMS. Users register in the application using their name and phone number. This phone number is then verified using a pin delivered via SMS. I find it difficult to determine whether it will be easier and / or better to use the phone_number model, and then create a connection between users and phone_numbers using belongs_to: and has_one: or if I can make the phone number part of the User model.

I am using Rails 4.2.1

Here is my user model:

class User < ActiveRecord::Base has_secure_password validates_presence_of :name validates_presence_of :phone_number validates_uniqueness_of :phone_number end 

User Controller:

 class UsersController < ApplicationController def new @user = User.new end def create @user = User.new(user_params) if @user.save session[:user_id] = @user.id redirect_to root_url, notice: "Saved" else render 'new' end end private def user_params params.require(:user).permit(:name, :phone_number, :password, :password_confirmation) end end 

This is the Twilio tutorial I'm trying to do:

http://twilioinc.wpengine.com/2015/06/sms-phone-verification-in-rails-4-using-ajax-and-twilio.html

Can I say this? Is the user really just a phone number with a name?

If I need to create a model / phone number controller, how do I change my routes? And how will this change my registration page, which now includes the following:

  <div class="field"> <%= f.label :phone_number %><br /> <%= f.text_field :phone_number %> </div> 
+4
source share
2 answers

You can make the phone_number model part of the user and simply use the [: user] [: phone_number] parameter to create it. It really depends on you and what user experience you are trying to build.

Does the user need to verify their phone number during registration? Should the user do this after creating the account?

In the first case, you can make the phone number a separate model belonging to the user. You put all the Twilio integration methods into model_number and use them in user_controller. You can make the verification step simpler by avoiding AJAX and setting it as the next step for user registration.

Here's what it might look like:

In your model:

  class PhoneNumber < ActiveRecord::Base belongs_to :user ... end 

In your user controller:

  def create @user = User.new(permitted_params) @phone_number = PhoneNumber.find_or_create_by(user_id: @user.id ,phone_number: params[:user][:phone_number]) @phone_number.generate_pin @phone_number.send_pin ... end 
+2
source

It really depends on the project you are working on, and on what you really want to do with your phone number. If this is just a simple case when the user has a phone number attached to him and you do not need to do any additional work with the phone number, then it is probably a good idea to save the phone number as an attribute of your user model.

But in this case, it has a somewhat complicated system. The phone number itself can have different attributes, such as number , pin and verfied , and you should have a form in which you can send a conclusion and make material based on these. In this case, it is definitely nice to create a separate phone model and add it to the user using the Rails association.

Again, this can be done anyway and really depends on the purpose and requirements of the project.

+1
source

All Articles