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