【问题标题】:Nested routes with Subdomain-fu使用 Subdomain-fu 的嵌套路由
【发布时间】:2010-03-16 13:12:23
【问题描述】:

我的应用中有一些标准的嵌套路由,我想使用 subdomain-fu gem 实现子域。所以我目前正在这样做:

example.com/stores/name_of_store/products/name_of_product

我想这样做:

name_of_store.example.com/products/name_of_product

似乎有一些关于 subdomain-fu 在 subdomain-fu 灯塔票据中嵌套路由的失败的讨论,但那个灯塔项目不再公开,所以我无法审查他们得出的任何结论。

很高兴听到人们关于您如何使用 subdomain-fu 实现嵌套路由的消息。

谢谢!

【问题讨论】:

    标签: ruby-on-rails routing subdomain nested-resources


    【解决方案1】:

    您根本不需要嵌套路由来实现这一点。您可以使用 subdomain_fu(或手动)查找 current_store,然后使用基本的 ProductsController 将其查找范围限定为商店内的产品:

    # ApplicationController
    def current_store
      @current_store ||= Store.find_by_subdomain(request.host)
    end
    helper_method :current_store
    
    # ProductsController
    def index
      @products = current_store.products.all
    end
    

    【讨论】:

    • 哇。多年来,我一直在寻找一种方法来清理我的白标域。起初我读了你的答案并想“等等,这与任何事情有什么关系”,但我盯着它看了 5 分钟,突然之间它变得有意义了!路由时不需要解释父资源实例。它可以以编程方式解释!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多