Ruby on rails 为每个页面设计身份验证
我网站上的每个页面都需要用户身份验证(用户必须登录才能访问)。我在Desive文档中找到了这个,但它似乎对我不起作用 我已经贴了很多了:Ruby on rails 为每个页面设计身份验证,ruby-on-rails,devise,Ruby On Rails,Devise,我网站上的每个页面都需要用户身份验证(用户必须登录才能访问)。我在Desive文档中找到了这个,但它似乎对我不起作用 我已经贴了很多了: authenticated :user do root :to => 'home#index' end root :to => redirect('/users/sign_in') 如何才能实现这一非常自然的功能?以下是我所做的: 在app/controllers/application\u controller.rb中,添加一行,
authenticated :user do
root :to => 'home#index'
end
root :to => redirect('/users/sign_in')
如何才能实现这一非常自然的功能?以下是我所做的:
在app/controllers/application\u controller.rb
中,添加一行,如下所示:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :authenticate_user!
# ...
end
require\u no\u authentication
函数还采用一个可选参数,一个名为only
的数组,其中包含不需要验证的操作列表
n、 b.我知道这是高级巫术,但问题没有答案,我花了相当多的时间寻找正确答案。你能解释一下哪个部分不适合你吗?它不会重定向到登录页面吗?是否重新启动服务器以使路由更改生效?它不会将我重定向到登录页面。我无需登录即可访问每个页面。是的,我已尝试重新启动服务器。您需要使用before_filter,我认为require_no_身份验证功能仅在从设备继承的控制器中可用,因此这不适用于其他控制器
class StaticPagesController < ApplicationController
prepend_before_filter :require_no_authentication
# ...
end