gpt4 book ai didi

ruby-on-rails - 如何在RoR 中限制用户访问页面?

转载 作者:数据小太阳 更新时间:2023-10-29 08:21:42 25 4
gpt4 key购买 nike

我知道我可以在用户登录后进行 session 。另外,我创建了一个名为“current_user”的辅助方法。我不希望其他未登录的人可以访问该页面。除了做这个,我还能怎么做?

我可以这样做来不允许人们访问内容,但我不希望登录中的用户也看到空白页面。

<% if current_user%>
My Content
<% end %>

最佳答案

创建一个 before_filter 检查 current_user 变量是否已设置,如果未设置则将用户重定向到其他地方(root、注册页面等)。像这样:

class ApplicationController < ActionController::Base
def login_required
redirect_to('/') if current_user.blank?
end
end

class MyController < ApplicationController
before_filter :login_required, :only => :action_aviable_only_for_logged_in

def action_aviable_only_for_logged_in
...
end
end

关于ruby-on-rails - 如何在RoR 中限制用户访问页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2122705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com