Reputation: 121
I want to add a serial number to a created user like I did here in a regular scaffold generation to a field named "code":
def create
@tester = Tester.new(tester_params)
@tester.code = @tester.created_at.to_s + ":" + rand(1235).to_s + ":" + rand(5123).to_s + ":" + rand(1523).to_s + ":" + SecureRandom.base64
respond_to do |format|
if @tester.save
format.html { redirect_to @tester, notice: 'Tester was successfully created.' }
format.json { render :show, status: :created, location: @tester }
else
format.html { render :new }
format.json { render json: @tester.errors, status: :unprocessable_entity }
end
end
end
I generated a devise user and copied in the devise controller code to override the devise session handler. So I think I overided the code with devise's own code but I can't find where the user is created, I've been looking in the devise registration code:
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message! :notice, :signed_up
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
set_minimum_password_length
respond_with resource
end
end
I have added_code_to_users, and now I want to do a current_user.code = secureRandom.base64 or something
Upvotes: 0
Views: 180
Reputation: 121
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
before_create :set_code
def set_code
self.code = SecureRandom.base64
end
end
Upvotes: 0
Reputation: 102240
The resource
(the user) is created in build_resource(sign_up_params)
.
However each action in Devise usually yields the resource.
yield resource if block_given?
This makes it pretty easy to extend simply by passing a block when calling super
.
class TesterRegistrationContoller < Devise::RegistrationsController
# ...
def create
super do |resource|
# This runs before `.save` is called on the user.
begin
# do you really need all that other stuff?
code = SecureRandom.uuid
end while Tester.exists?(code: code)
resource.code = code
end
end
end
Upvotes: 2