Use [Rails] devise Guest user function (for portfolio)

at first

Guest user login function that is said to be essential for portfolio websites. Introducing the guest login function using devise. As a premise

The introduction of devise has been completed The following is a reference article. ・ Htps: // Quiita. This m / Shinyakato / Te ms / A 098 A 741 A 142616 A 753

Implementation of guest login function

config.routes.rb


#Add the following
 devise_for :users, :controllers => {
    registrations: 'users/registrations',
    sessions: 'users/sessions'   
  } 

devise_scope :user do
    post 'users/guest_sign_in', to: 'users/sessions#new_guest'
  end

app.controllers.users.sessions.controller.rb



#Add the following
 def new_guest
    user = User.find(1)
    user.update(email: '[email protected]',name: 'Guest user') do |user|
      user.password = SecureRandom.urlsafe_base64
    end
    sign_in user
    redirect_to root_path
  end

app.views.tweets.index.html.haml


#Add the following
.guest
  = link_to  users_guest_sign_in_path, method: :post, class:"guest__btn" do
The guests

point

-Use the sign_in method of devise.

-By using find_by, you can save the trouble of creating guest users in advance.

-Password is randomly created by using SecureRandom.urlsafe_base64. This will prevent the password from being leaked when you upload the source code to GitHub.

Supplement

The content I introduced is just a way to register guest users. If you want to prevent the account deletion function and password change, you need to set it as a plus to the contents introduced so far.

Recommended Posts

Use [Rails] devise Guest user function (for portfolio)
Implement user follow function in Rails (I use Ajax) ②
Rails application guest login function implemented (devise not used)
Implement user follow function in Rails (I use Ajax) ①
[Rails] Implement User search function
(For beginners) [Rails] Install Devise
Memorandum [Rails] User authentication Devise
Implement user registration function and corporate registration function separately in Rails devise
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] Implementation of user withdrawal function
[Rails] Function restrictions in devise (login / logout)
[Rails] Unit test code for User model
Implementation of user authentication function using devise (2)
Creating a user authentication function using devise
Implementation of user authentication function using devise (1)
Rails [For beginners] Implementation of comment function
[Rails] Use devise to get information about the currently logged in user
Implementation of user authentication function using devise (3)
[Rails] devise
[Note] How to use Rails 6 Devise + cancancan
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
Validation settings for Ruby on Rails login function
Create authentication function in Rails application using devise
[For beginners] Test devise user registration with RSpec
Implement user edit / update function without using devise
[Rails 6.0] "Easy login" implementation procedure required for portfolio
Add a tag function to Rails. Use acts-as-taggable-on
[Rails] How to use Gem'rails-i18n' for Japanese support
[Note] Summary of rails login function using devise ①
Use pagy for pagination in your Rails app.
[Rails 6] Ranking function
devise user registration
[Rails] Category function
[Rails] Introducing devise
Rails follow function
rails + devise + devise_token_auth
[Rails] Add Tags that are related to has_many to Devise User (addition of function to follow tags)
[Rails] Use jQuery
[Rails] Notification function
How to implement guest login in 5 minutes in rails portfolio
[rails] How to use devise helper method before_action: authenticate_user!
Implementation of Ruby on Rails login function (devise edition)
Introduce devise in Rails to implement user management functionality
[Rails Tutorial] "NoMethodError (undefined method` activation_digest ='for # <User: 0x00000003156938>
[For Rails beginners] Implemented multiple search function without Gem
What should I use for the testing framework [Rails]
I made a reply function for the Rails Tutorial extension (Part 4): A function that makes the user unique