Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 仅限访客访问页面,即阻止登录用户访问页面(Rails 4,Desive)_Ruby On Rails_Ruby On Rails 3_Devise - Fatal编程技术网

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倍!