[RUBY] Rails access restrictions

type

Access restrictions for users who are not logged in Access restrictions for logged-in users Description in view file

Specific procedure

Access restrictions for users who are not logged in

Write before_action in the controller

***controller.rb


before_action :forbid_login_user, {only: [:top]}

#~Other descriptions are omitted~
def autheniticate_user
  if @current_user.nil?
    flash[:notice]="Please login"
    redirect_to("/URL")
  end
end

Access restrictions for logged-in users

Write before_action in the controller

***controller.rb


before_action :forbid_login_user, {only: [:top]}

#~Other descriptions are omitted~

def fobid_login_user
  if @current_user
    flash[:notice]="You are logged in"
    redirect_to("/URL")
  end
end

Example to describe in view

Suppose you are using devise.

***.html.erb


<% if user_signed_in? && current_user.id == @usesinfo.usersid %>
  <li>
    <%= link_to 'edit', "/URL", method: :get %>
  </li>
  <li>
    <%= link_to 'delete', "/URL", method: :delete %>
  </li>
<% end %>

Recommended Posts

Rails access restrictions
[Rails g.error]
Rails basics
Rails Review 1
[Rails] Function restrictions in devise (login / logout)
[Rails] first_or_initialize
rails tutorial
Authentication function in Play Framework [Access restrictions]
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
[Rails] devise
rails tutorial
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
[Rails] Access signed / encrypted cookies with request spec