简体   繁体   English

将页面访问权限限制为仅访客访问者,即阻止已登录的用户访问页面(Rails 4,devise)

[英]Restrict access to page to only guest visitors ie prevent logged-in users to access a page (Rails 4, devise)

I have a question a little weird: I'd like to restrict the access to a page to only NON logged-in users. 我有一个奇怪的问题:我想将对页面的访问限制为仅NON个登录用户。

Should I do this like this: 我应该这样做吗?

class StaticPagesController < ApplicationController

  def globalpresence 
    redirect_to root_path if user_signed_in?
  end

end

Is the right way to do this ? 是正确的方法吗? Is there a cleaner/more proper way to that with Rails or devise? 有没有更干净/更合适的方法来使用Rails或devise?

Thanks 谢谢

A before filter would probably be more flexible in the long run 从长远来看,前置过滤器可能会更灵活

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

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

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