I set root_path to something that looks like a ** home screen ** that I want to show after logging in. Users who are not logged in will be set to jump to the ** app introduction page **
As written on this page, it uses ** devise **. If you haven't installed devise yet, please do so before returning to this page. Now let's get into the main subject First, change the description of the controller.
photos_controller.rb
class PhotosController < ApplicationController
before_action :move_to_index #Add here
def index
end
#Add the following from here
private
def move_to_index
unless user_signed_in?
redirect_to controller: :homes, action: :index
end
end
end
I will explain what you are doing from above before_action :move_to_index before_action is used to do something before all the actions of the controller are executed. A method called move_to_index is specified for that before_action. Since it is a method, the description here is different for everyone So where do you define that method? ?? I will explain it later.
private A method that cannot be called from outside the class. In Ruby, the following code described as private is a private method. My interpretation is that it is a method that can only be used within this controller. I'm sorry if it's different ...
def move_to_index Move_to_index is defined here. It leads to before_action: move_to_index earlier. Let's see what the move_to_index is doing
unless user_signed_in? user_signed_in? is a helper method that can be used by inserting devise ** Determine if the user is logged in ** It means like if there is no unless ~ there When this word comes, if you are not logged in, go to the next line with ~
redirect_to controller: :homes, action: :index Because the sentence is a little long First of all, pay attention to ** redirect_to ** This is a method ** that can transition to the specified URL ** So
controller.rb
redirect_to "http://www.○○○○.com"
But you can
controller.rb
redirect_to root_path
You can also specify the Prefix name like
controller.rb
redirect_to controller: :homes, action: :index
You can also specify the action of the last specified controller
There are other things you can do, so please search for them.
This is the flow ** Read the mobe_to_index method first with before_action before the index is loaded. If the user is not logged in, you can set it by transitioning to here. ** **
With the controller
redirect_to controller: :homes, action: :index
Is described at the end The index action of the homes controller is called
routes.rb
get '/homes/home', to:'homes#index'
To describe
** / homes / home is the URL name specification **
It looks like this.
And ** homes # index **
Specifies the index action for the homes controller
That's all