I am creating a portfolio using Ruby on Rails in order to change jobs from inexperienced to IT engineer.
When creating a portfolio that implements the login function, most of the functions cannot be used without logging in, so you must log in to confirm the function (check your user name and password, or register a new one). It takes time and effort.
Originally, it is a function that is not necessary as an application, but as part of studying, and when you look at your portfolio, we will implement a function that allows you to easily log in so that the employer does not have to take time.
Ruby:2.6.3 Ruby on Rails:5.2.0
I will attach an article that I referred to when implementing the function. -[Easy login / guest login function implementation method (for portfolio)](https://qiita.com/take18k_tech/items/35f9b5883f5be4c6e104 ") It was very helpful. I think that there is no problem if you can refer to here for the basics. Thank you very much. In my case, I didn't use a feature called Devise, so I implemented it in a way that doesn't use Devise. It is assumed that the user is processing with user Model and controller.
Terminal
$ rails g controller sessions
Create SessionsController above
/app/controller/sessions_controller.rb
class SessionsController < ApplicationController
def create
user = User.find_by(name: params[:name])
if user&.authenticate(params[:password])
session[:user_id] = user.id
redirect_to :root
else
render template: "users/login_form"
end
end
def destroy
session.delete(:user_id)
redirect_to :root
end
#The following guest login actions
def guest_login
user = User.guest
session[:user_id] = user.id
redirect_to :root, notice: "You have logged in as a guest user."
end
end
Since create and destroy are necessary when managing the login function with the sessions controller, they are described. Redirect and render destination can be set arbitrarily. Personal note: &. Returns nil when the left side object is nil, and calls the method on the right side when it is not nil.
The action name can be set arbitrarily. The guest method is defined in the User Model in the next section.
/app/assets/models/user.rb
#abridgement
def self.guest
find_or_create_by!(name: 'guest') do |user|
user.assign_attributes({
email: "[email protected]",
birthday: "1990-01-01",
sex: 3})
user.password = SecureRandom.urlsafe_base64
end
end
In my case, the user table has several columns, so when I log in to guest, I use assign_attributes to embed various information. It can be omitted if it is not necessary.
/config/routes.rb
#abridgement
resource :session, only: [:create, :destroy] do
post "guest_login", on: :collection
end
Set the routing to guest_login.
:/app/assets/views/~/view.html.erb
<%= link_to "Easy login", :guest_login_session, method: :post %>
Embed the above where needed.
With the above, the simple login function can be implemented. The point is ** find_or_create_by! ** in Section 3. You can search for'guest' from the argument name, return the corresponding data if it exists, and create a new one if it doesn't. With this setting, even if the guest user is deleted, you can log in again as'guest'.
To some extent, I created the application and then introduced it additionally, so I think there are some scenes where the explanation is missing. We apologize for any inadequacies. I would appreciate it if you could implement it in a way that suits you by referring to the articles I referred to and other articles.
This is my first post, but when I wrote the article, I had many opportunities to reconfirm the meaning of the words, which was a great learning experience. I reconfirmed that communicating what I learned will deepen my understanding. In the future, as well as studying, I would like to do my best to post.
Recommended Posts