I have a devise model called user. When a user signs up, they will be directed to fill out form called "userinfo". I have a model called userinfo. As soon as a new userinfo is created, I give each userinfo a unique token. I permit "token" in my userinfo controller. It works, but everytime I edit the form and update, the unique token changes too. I was thinking I should only show the first created token on the userinfo#show page. But if a user updates their userinfo form 5 times, 5 tokens will be created and 4 will be wasted.
So actual problem: Create unique token when userinfo#new happens and show it on the userinfo#show page. Unique token should not be updated when userinfo#edit and userinfo#update happens.
My userinfo model:
class Userinfo < ActiveRecord::Base
belongs_to :user
before_save :set_token
def set_token
self.token = rand(100000..999999)
end
end
Userinfo controller:
class UserinfosController < ApplicationController
before_action :find_userinfo, only: [:show, :edit, :update, :destroy, :log_impression]
before_action :authenticate_user!
def index
@userinfors = Userinfo.search(params[:search])
end
def show
end
def new
@userinformation = current_user.build_userinfo
end
def create
@userinformation = current_user.build_userinfo(userinfo_params)
if @userinformation.save
redirect_to userinfo_path(@userinformation)
else
render 'new'
end
end
def edit
end
def update
if @userinformation.update(userinfo_params)
redirect_to userinfo_path(@userinformation)
else
render 'edit'
end
end
def destroy
@userinformation.destroy
redirect_to root_path
end
private
def userinfo_params
params.require(:userinfo).permit(:name, :email, :college, :gpa, :major, :token, :skills, :user_img)
end
def find_userinfo
@userinformation = Userinfo.friendly.find(params[:id])
end
end
View:
<%= @userinformation.token %>
SecureRandom.random_number*10000000000000000
:) – whodini9