gpt4 book ai didi

ruby-on-rails - 是否可以在 Rails 中指定两个根页面(一个用于匿名用户,另一个用于登录用户)

转载 作者:行者123 更新时间:2023-12-02 01:03:53 25 4
gpt4 key购买 nike

我正在构建一个具有静态页面和动态页面(产品相关)的产品。这两类页面都有不同的发布生命周期。营销团队与设计师合作,发布静态页面,产品页面由工程团队发布。

静态页面驻留在public/home中,并且它们是独立的。除了提供链接之外,他们不需要访问 Rails 基础设施。

在此设置中,我尝试实现以下行为:

  • 当未经身份验证的访问者启动 http://www.xyz.com 时,用户应该被带到静态登陆页面。

  • 当经过身份验证的访问者启动 http://www.xyz.com 时,用户应该被带到产品登陆页面(LandingsController,索引操作)。

在我当前的实现中,我检查用户是否在 Rails 世界中经过身份验证并呈现静态页面或产品页面。

我想知道以下内容:

1)您如何处理此类情况?

2)有没有办法避免进入静态主页的Rails堆栈。

3)是否可以自定义root_path方法以根据上下文返回不同的根

最佳答案

1) How do you handle such scenarios?

常见的答案如下:

class LandingsController < ApplicationController

before_filter :login_required

def index
...
end

...

private

def login_required
if not_logged_in? # This methods depends on your authentication strategy
send_file "/your/static/path/#{params[:action]}", :type => "application/html charset=utf8;"
return false # Halt chain
end
end

send_file documentation

并且,根据每个操作与模板之间的对应关系,您可以进一步将 login_required 方法抽象到 ApplicationController 中,并验证文件是否存在。

2) Is there a way to avoid entering the Rails stack for static pages

是的。你必须相信我的话,因为我自己没有做过,但你可以使用 Rack middleware要做到这一点。 Here is an example如何做类似的事情,不同之处在于您将静态地提供文件而不是重定向(只需将 header 和 File.read 的结果设置为内容),但这取决于您正在使用的身份验证库.

3) Is there a customization for the root_path method to return different root based on the context

您无法定义条件路由(即在 routes.rb 文件中定义多个路由),但您可以覆盖 ApplicationController 中的 root_url 方法,假设您在路由定义中使用命名路径root。类似的东西

class ApplicationController

def root_url(*options)
if logged_in?
"/return/something/custom"
else
super(*options)
end
end

end

然而,这听起来确实是一个坏主意,因为 1)您应该指向相同的 url,并让 Controller 处理请求(您的链接应该不知道将您带到哪里),并且 2)它可能会破坏依赖 root_urlroot_path 方法的其他内容。

关于ruby-on-rails - 是否可以在 Rails 中指定两个根页面(一个用于匿名用户,另一个用于登录用户),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3664405/

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