I will explain how to implement the "guest login function" that can be said to be essential for portfolios for job change activities. There are various ways to do it, but we will implement it in the way that seems to be the easiest. We will proceed on the assumption that the login function using devise is implemented (as long as the hiring manager can log in).
① Register guest user information in the seed file ② Add guest column (boolean type) to Users table. This is so that it can be determined whether or not it is a guest user. ③ Read the seed file after migrating ④ Creation of guest login button
db/seeds.rb/
User.create!(name: 'Guest User',
email: '[email protected]',
password: '12345678',
password_confirmation: '12345678',
created_at: Time.zone.now,
updated_at: Time.zone.now,
guest: true)
If there are columns other than name, email and password, add them in the same way. guest: The true part is the newly added column described below.
db/seeds.rb/
class DeviseCreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.string :name, null: false
##Added below! !!
t.boolean :guest, default: false, null: false
##Omitted below
end
end
Set default values for boolean columns. By setting default: false, normal users will be guest: false and can be distinguished from administrative users. (Fal is returned in user.guest, true is returned only for guest users.) Null: false is not required, but it is not good to have null in the database, so it is tentative.
Execute the following commands in order.
rails db:migrate:reset
rails db:seed
If "Gust User" is registered as shown below and 1 is entered in the gust column, it is successful. 
If you come this far, you will have a rest.
ruby:app/views/devise/sessions/new.html.erb
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |g| %>
<%= g.hidden_field :email, value: '[email protected]' %>
<%= g.hidden_field :password, value: '12345678' %>
<div class="mb-3">
<%= g.submit 'Guest login', class: 'btn btn-warning' %>
</div>
<% end %>
You can hide the form by using g.hidden_field, submit the form information and log in as a guest user with the push of a button! Place this button in any position and you're done. Thank you for your hard work.
Recommended Posts