Ruby on rails 仅限访客访问页面,即阻止登录用户访问页面(Rails 4,Desive)
我有一个有点奇怪的问题:我想限制只有未登录的用户才能访问页面 我应该这样做吗:Ruby on rails 仅限访客访问页面,即阻止登录用户访问页面(Rails 4,Desive),ruby-on-rails,ruby-on-rails-3,devise,Ruby On Rails,Ruby On Rails 3,Devise,我有一个有点奇怪的问题:我想限制只有未登录的用户才能访问页面 我应该这样做吗: class StaticPagesController < ApplicationController def globalpresence redirect_to root_path if user_signed_in? end end class StaticPagesController
class StaticPagesController < ApplicationController
def globalpresence
redirect_to root_path if user_signed_in?
end
end
class StaticPagesController
这样做正确吗?
有没有更干净/更合适的方法来使用导轨或装置
谢谢从长远来看,before过滤器可能会更灵活
before_filter :only_allow_guest_users, only: :global presence
def globalpresence
end
def only_allow_guest_users
redirect_to root_path if user_signed_in?
end
这看起来并不太糟糕,但我会将名称更改为
check\u global\u presence
(更具可读性并说明它的功能),并在\u操作之前添加:check\u global\u presence
,或者如果是当前用户?但我认为用户登录了?读得更好。我只是觉得你在代码中犯了一个错误,它是在过滤器之前的,而不是在过滤器之前的:@Mathieu肯定,很好的捕获!在Rails 4.1+中,这将是在您采取行动之前,所以我错了2倍!