简体   繁体   English

当用户未登录网页时,已将设计添加到用户,将直接进入Rails的登录页面

[英]Devise added to user, when user is not logged in web page is directly going to login page in rails

I'm working on a rails application when I run localhost:3000, if I am not logged in, it directly goes to login page. 当我运行localhost:3000时,我正在使用Rails应用程序,如果我没有登录,它将直接进入登录页面。 according to the requirement, I should get a welcome statement which is mentioned on the home page. 根据要求,我应该获得主页上提到的欢迎声明。

my home page 我的主页

    <h1>Cricket Admin Page </h1>

<% if user_signed_in?   %>
    <h2><%= link_to "Profile", user_path(current_user)  %></h2>
    <h2><%= link_to "Add role" , new_role_path %></h2>
    <h2> <%= link_to "Modify Users", users_path%></h2>  
<% else %> 
    <h1><% Welcome to cricket App %></h1>
    <h2><% to move further please login %></h2>
<% end %>

route.rb route.rb

Rails.application.routes.draw do


 devise_for :users, controllers: { registrations: 'users/registrations' }

  resources :users 

  root 'home#index'
end

Please remove before_action :authenticate_user! 请删除before_action :authenticate_user! from your application_controller 从您的application_controller

Add like this in home_controller 像这样在home_controller添加

before_action :authenticate_user!, except: [:index]

or 要么

before_action :authenticate_user!, only: ["list all actions except index"]

You can add before_action :authenticate_user! 您可以添加before_action :authenticate_user! to your application controller if you want to manage authentication in every controller that inherits from ApplicationController. 如果要管理从ApplicationController继承的每个控制器中的身份验证,请转到应用程序控制器。

Then, in your individual controllers, where you don't need a user to be authenticated, you can use skip_before_action :authenticate_user! 然后,在不需要对用户进行身份验证的单个控制器中,可以使用skip_before_action :authenticate_user! and use only: or except: to skip authentication on those actions. only:使用only:except:跳过对这些操作的身份验证。

For instance, to skip user authentication on your home_controller index action, you can use skip_before_action :authenticate_user!, only: %i[index] in your home_controller.rb file. 例如,要跳过home_controller索引操作上的用户身份验证,可以skip_before_action :authenticate_user!, only: %i[index]home_controller.rb文件中使用skip_before_action :authenticate_user!, only: %i[index]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM