[RUBY] How to constrain the action of the transition destination when not logged in

background

This time, I will introduce a method to restrict the transition so that users who are not logged in can only transition to the specified page.

Thing you want to do

Users who are not logged in can only transition to the index and show pages, and if they try to transition to the new page or edit page, they will be forced to transition to the index page.

How to use

Code that is used repeatedly in the controller is made into a method under private. 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

that's all

Recommended Posts

How to constrain the action of the transition destination when not logged in
[rails devise] How to transition users who are not logged in to the login page
How to find the total number of pages when paging in Java
How to set when "The constructor Empty () is not visible" occurs in junit
[Java] How to get the URL of the transition source
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Summary of how to use the proxy set in IE when connecting with Java
How to set chrony when the time shifts in CentOS7
How to get the log when install4j does not start
How to derive the last day of the month in Java
How to solve the problem when the value is not sent when the form is disabled in rails and sent
Disable display when not logged in
[Rails] How to get the URL of the transition source and redirect
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to solve the unknown error when using slf4j in Java
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
How to write the view when Vue is introduced in Rails?
How to prevent users who are not logged in from transitioning to the new or edit page
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
Method definition location Summary of how to check When defined in the project and Rails / Gem
[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 sort the List of SelectItem
How to get the date in java
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
How to set the indent to 2 single-byte spaces in the JAXB implementation of the JDK
How to change the maximum and maximum number of POST data in Spark
[Rails] How to get the user information currently logged in with devise
How to not start Flyway when running unit tests in Spring Boot
How to change the value of a variable at a breakpoint in intelliJ
How to get the absolute path of a directory running in Java
[Rails] The cause of not being able to post was in form_with
Android development, how to check null in the value of JSON object
[Ruby] How to prevent errors when nil is included in the operation
How to reference a column when overriding the column name method in ActiveRecord
Change the save destination of the image to S3 in the Rails app. Part 2
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
[Swift] How to get the number of elements in an array (super basic)
Summary of how to select elements in Selenium
[Rails] How to decide the destination by "rails routes"
[swift] How to control the behavior when the back button of NavigationBar is pressed
How to find the cause of the Ruby error
[Ruby on Rails] How to make the link destination part of the specified id
graphql-ruby: How to get the name of query or mutation in controller Note
How to check the logs in the Docker container
Customize how to divide the contents of Recyclerview
How to get the ID of a user authenticated with Firebase in Swift
When the server fails to start in Eclipse
How to check the latest version of io.spring.platform to describe in pom.xml of Spring (STS)
[Error] How to resolve the event that the screen does not transition after editing
How to add sound in the app (swift)
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
How to get today's day of the week
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
How to delete the database when recreating the application
How to resolve the error'ActionView :: Template :: Error (The asset "application.css" is not present in the asset pipeline.'" When precompiling Rails assets
How to use JQuery in js.erb of Rails6
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to display the result of form input