[RUBY] [rails devise] How to transition users who are not logged in to the login page

Premise: This time, we will operate with the items controller.

class ItemsController < ApplicationController
  before_action :move_to_signed_in, except: [:index]
  def index
    #Top page generation

  def move_to_signed_in
    unless user_signed_in?
      #Users who are not signed in will see the login page
      redirect_to  '/users/sign_in'

How to use before_action

before_action :move_to_signed_in, except: [:index]

before_action → A method that can perform the specified common processing before the action defined in the controller is executed. except: [: index] → index action is excluded -When other actions are executed except the index action, move_to_signed_in is processed. In other words, when the top page is displayed, the move_to_signed_in process is not executed.

Execution content of move_to_signed_in

def move_to_signed_in
    unless user_signed_in?
      #Users who are not signed in will see the login page
      redirect_to  '/users/sign_in'

user_signed_in? → Determine if the user is logged in unless → "if not" unless user_signed_in? → If the user is not logged in redirect_to'/ users / sign_in' → Transit to login page


If the user tries to perform an action other than the index action (such as create) Determine if you are a logged-in user. If the user is not logged in, the screen will change to the login page (/ users / sign_in). If you can log in, you can perform the action specified by the user.

Recommended Posts

[rails devise] How to transition users who are not logged in to the login page
How to prevent users who are not logged in from transitioning to the new or edit page
Memo that transitions to the login screen if you are not logged in with devise
[Rails] How to get the user information currently logged in with devise
How to constrain the action of the transition destination when not logged in
[Rails] Use devise to get information about the currently logged in user
[Rails] How to temporarily save the request URL of a user who is not logged in and return to that URL after logging in
[Rails] Added in devise: username not added to database
How to check Rails commands in the terminal
[Rails] Give this article to you who looked up in "devise name login"
How to implement guest login in 5 minutes in rails portfolio
[Rails] How to display an image in the view
[Ruby On Rails] Description that allows only specific users to transition to the edit page
[Rails] How to install devise
Super easy in 2 steps! How to install devise! !! (rails 5 version)
[Rails] How to display information stored in the database in view
How to solve the problem when the value is not sent when the form is disabled in rails and sent
[Rails] How to use gem "devise"
[Rails / Routing] How to refer to the controller in the directory you created
[Rails] How to use devise (Note)
[Rails carrier wave] How to not transition to the error screen even if the image upload is not selected
[rails] Login screen implementation in devise
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
What to do when the changes in the Servlet are not reflected
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
[Rails] How to change the page title of the browser for each page
How to introduce jQuery in Rails 6
[Rails] How to reset the database in production environment (Capistrano version)
[Rails 5] How to display the password change screen when using devise
How to write the view when Vue is introduced in Rails?
How to install Swiper in Rails
[Ruby on Rails] How to log in with only your name and password using the gem devise
[Rails] How to log in with a name by adding a devise name column
How to implement more than one top page in Rails breadcrumb trail
How to switch the display of the header menu for each transition page
How to update user edits in Rails Devise without entering a password
How to debug the processing in the Ruby on Rails model only on the console
How to display the text entered in text_area in Rails with line breaks
[Rails] The cause of not being able to post was in form_with
[Rails] How to apply the CSS used in the main app with Administrate
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
How to insert a video in Rails
[Rails] How to use the map method
How to use MySQL in Rails tutorial
[rails] How to configure routing in resources
How to get the date in java
How to implement ranking functionality in Rails
[Note] How to use Rails 6 Devise + cancancan
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to translate devise into Japanese
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
How to resolve the error'ActionView :: Template :: Error (The asset "application.css" is not present in the asset pipeline.'" When precompiling Rails assets
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
[For rails beginners] Specify transition destination after logging in to multiple Devise models
[Rails] How to register multiple records in the intermediate table with many-to-many association
What to do when rails db: seed does not reflect in the database
How to resolve errors that occur in the "Ruby on Rails" integration test
[Error] How to resolve the event that the screen does not transition after editing