[RUBY] How to implement guest login in 5 minutes in rails portfolio

Introduction

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).

flow

① 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

1 Seed file editing

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.

2 Add guest column

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.

3 Read file

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. ![983D61C1-739E-4B1C-9DB6-A1047C184855_4_5005_c.jpeg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/694973/5fa8a35c-df9f-26d0-271b-8591eb9e32b2 .jpeg)

4 Creating a guest login button

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

How to implement guest login in 5 minutes in rails portfolio
How to implement search functionality in Rails
How to implement ranking functionality in Rails
How to implement a like feature in Rails
[Rails] How to implement scraping
How to implement login request processing (Rails / for beginners)
How to implement a like feature in Ajax in Rails
[Rails] How to write in Japanese
Implement simple login function in Rails
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
How to install Swiper in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
How to use MySQL in Rails tutorial
How to implement coding conventions in Java
[rails] How to configure routing in resources
How to implement image posting using rails
How to implement asynchronous processing in Outsystems
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to implement a slideshow using slick in Rails (one by one & multiple by one)
How to implement more than one top page in Rails breadcrumb trail
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
[Rails] How to use select boxes in Ransack
[Swift] How to implement the LINE login function
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
[Rails] How to implement unit tests for models
How to separate .scss by controller in Rails
How to conditionally add html.erb class in Rails
How to easily create a pull-down in Rails
How to use JQuery in js.erb of Rails6
[Rails] How to easily implement numbers with pull-down
How to implement optimistic locking in REST API
[Ruby on Rails] How to install Bootstrap in Rails
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to implement Pagination in GraphQL (for ruby)
How to write Rails
How to uninstall Rails
Implement markdown in Rails
How to implement image posting function using Active Storage in Ruby on Rails
How to implement UICollectionView in Swift with code only
How to set the display time to Japan time in Rails
[Ruby on Rails] How to write enum in Japanese
[Rails, JS] How to implement asynchronous display of comments
Introduce devise in Rails to implement user management functionality
Understand how to use Swift's JSON Decoder in 3 minutes
[Ruby On Rails] How to reset DB in Heroku
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
Summary of how to implement default arguments in Java
Rails learning How to implement search function using ActiveModel
[Rails] How to load JavaScript in a specific view