Users who are not logged in can only transition to the index and show pages, and when they try to transition to the new page or edit page, they are forced to transition to the index page.
By performing the following processing, if you try to transition to a page other than the index and show pages while the user is not logged in, you will be forced to transition to the index page.
controller.rb
class PracticeController < ApplicationController
before_action :move_to_index, except: [:index, :show]
---abridgement---
private
def move_to_index
redirect_to action: :index unless user_signed_in?
end
Code that is used repeatedly in the controller is made into a method under private.
Thank you for watching till the end! !!
I hope you found this article helpful: pray_tone2:
Recommended Posts