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
end
def show
end
end
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.
user_signed_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" %>
</div>
<% else %>
<div class="grid-6">
<%= link_to "Login", new_user_session_path, class: "post" %>
<%= link_to "sign up", new_user_registration_path, class: "post" %>
</div>
<% 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