[RAILS] Helper methods available in devise

Method Use
before_action :authenticate_user! Set in the controller to allow access only to users.
user_signed_in? Determine if the user is logged in. Returns true if the user is logged in, false if logged out.
current_user Get the currently logged in user
user_session Access user session information

before_action :authenticate_user! A devise method that switches the page to be displayed depending on the login status.

class SampleController < ApplicationController
  before_action :authenticate_user!, only: [:show]

  def index

  def show

With the only option, the show action is accessible only to logged-in users, and the index action is accessible even if you are not logged in.


<% if user_signed_in? %>
  <div class="user_nav grid-6">
    <%= link_to "Logout", destroy_user_session_path, method: :delete %>
    <%= link_to "Post", new_tweet_path, class: "post" %>
<% else %>
    <div class="grid-6">
      <%= link_to "Login", new_user_session_path, class: "post" %>
      <%= link_to "sign up", new_user_registration_path, class: "post" %>
<% end %>

If you are signed in, you can display "Logout" and "Post" pages, and if you are not signed in, you can display "Login" and "New registration".

As a reminder, as the devise settings are still unfamiliar

Recommended Posts

Helper methods available in devise
About methods often used in devise
[Rails] devise helper method
Dynamically call methods in JSF
Enable strong parameters in devise
Test private methods in JUnit
Test private methods in JUnit
Make html available in Vapor
Mock static methods in Mockito 3.4
[Rails] Session timeout setting in devise
[rails] Login screen implementation in devise
[Rails] Unexpected validation error in devise
Differences in split methods of StringUtils
Set up pre-login routing in Devise
Frequently used methods in Active Record
all-ruby is now available in ghcr.io/ruby/all-ruby
Ruby methods often used in Rails