[RUBY] How to prevent users who are not logged in from transitioning to the new or edit page

What I want to do this time

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.

Method

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

Description

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

How to prevent users who are not logged in from transitioning to the new or edit page
[rails devise] How to transition users who are not logged in to the login page
Prevent other users from going to the post edit screen
Prevent login users from transitioning to a specific page by entering a URL
How to constrain the action of the transition destination when not logged in
Memo that transitions to the login screen if you are not logged in with devise
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
How to handle items in GSON that do not know whether they are single or array
How to prevent past dates from being entered in Rails forms
What to do when the changes in the Servlet are not reflected
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to develop from VScode in a remote destination environment or a remote destination container environment
[Rails] How to get the user information currently logged in with devise
How to get the setting value (property value) from the database in Spring Framework
[Ruby] How to prevent errors when nil is included in the operation
How to get the date in java
graphql-ruby: How to get the name of query or mutation in controller Note
What to do if the changes are not reflected in the jar manifest file
How to set when "The constructor Empty () is not visible" occurs in junit
How to get the date from the JavaScript Date type that C # developers are addicted to