Ruby-on-rails 检查用户是否在 Rails 中的任何操作之前登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30691878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if user signed in before any action in Rails
提问by yerassyl
I want to execute some function before any controller action to check if the user is signed in. I am using deviseso that I can use is_signed_in?, but I have to put if else condition to every method in the controller.
我想在任何控制器操作之前执行一些函数来检查用户是否已登录。我正在使用devise以便我可以使用is_signed_in?,但我必须将 if else 条件放在控制器中的每个方法中。
What I want is to have something like this:
我想要的是有这样的东西:
#some_controller.rb
before_action :is_signed_in?
def is_signed_in?
if !user_signed_in?
redirect_to new_user_session_path
else
..proceed to the action intended to call
end
end
回答by Florent Ferry
Deviseis shipped with some useful built-in helpers.
Devise附带了一些有用的内置帮助程序。
In your case, one that interested you is authenticate_user!. Take a look at controller filters and helpersin Devise documentation.
就您而言,您感兴趣的是authenticate_user!. 查看Devise 文档中的控制器过滤器和助手。
You can filter your actions in your controller with this method to ensure only logged-in users can process a given action or all actions in a controller, else if user isn't logged-in then he is redirect to login page.
您可以使用此方法过滤控制器中的操作,以确保只有已登录的用户才能处理给定的操作或控制器中的所有操作,否则如果用户未登录,则将其重定向到登录页面。
before_action :authenticate_user!
before_action :authenticate_user!, only: [:show]
回答by Archie Reyes
You can also create your own helper method.
您还可以创建自己的辅助方法。
In your users_controller.rb, create a before_action filter
在您的 中users_controller.rb,创建一个 before_action 过滤器
class UsersController < ApplicationController
before_action :logged_in_user
...
end
and in your session_helper.rb
并且在你的 session_helper.rb
module SessionHelper
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
# Confirms a logged-in user.
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
end
回答by webster
If you want to check whether user is signed for every action in the application, you have to put the filter in the application controller. You can do this for a specific controller also.
如果要检查用户是否为应用程序中的每个操作签名,则必须将过滤器放在应用程序控制器中。您也可以为特定控制器执行此操作。
You can use the devisemethod:
您可以使用以下devise方法:
class SomeController < ApplicationController
before_action :authenticate_user!
...
end
You can create your own filter also:
您也可以创建自己的过滤器:
class SomeController < ApplicationController
before_action :my_authentication
...
def my_authentication
if user_signed_in?
# do something ...
else
# do something else ...
end
end
end
回答by Kent Aguilar
You can add this method to your ApplicationController
您可以将此方法添加到您的 ApplicationController
def user_is_logged_in
if !session[:current_user]
redirect_to login_path
end
end
Use it before invoking any actions. Like so,
在调用任何操作之前使用它。像这样,
class AdminsController < ApplicationController
before_action :user_is_logged_in
...
end
回答by rmagnum2002
Are you using devise? You can use existing filter:
你在使用设计吗?您可以使用现有过滤器:
class SomeController < ApplicationController
before_filter :authenticate_user!
...
end
if not, create your filter in application controller and add it to your needed controllers:
如果没有,请在应用程序控制器中创建过滤器并将其添加到您需要的控制器中:
class SomeController < ApplicationController
before_filter :my_auth_filter
...
end

