【问题标题】:Rails Routing: Flexible Resource Path depending on attributesRails 路由:取决于属性的灵活资源路径
【发布时间】:2017-09-21 03:31:57
【问题描述】:

如何根据其属性使资源路由足够灵活?

例如,

resources :articles, param: :article_slug do
    member do
      resources :comments
    end
end

Article 具有title, category, slug, etc 的属性

但客户希望类别必须是 url 中的第一个出现,例如:/entertainment/articles/:article_slug/comments/:id/sports/articles/:article_slug/comments/:id。我不知道这个方法的正确方法是什么。

【问题讨论】:

  • 你现在有什么网址?运行后rake routes
  • 我只有/articles/:article_slug/comments/:id

标签: ruby-on-rails ruby-on-rails-4 routes custom-routes


【解决方案1】:

试试

scope path: ':category' do
  resources :articles, param: :article_slug do
      resources :comments
      end
  end
end

并以这种方式生成url,

article_comment_path(@article, @comment, category: @article.category)

【讨论】:

  • 是的,它可以工作,但类别是Article 的属性。我正在尝试使用一些 lambda 方法,但它不起作用。
  • @JoseMarieAntonioMiñoza 我已经更新了答案,你可以检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 1970-01-01
  • 2011-01-17
  • 2016-11-12
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
相关资源
最近更新 更多