Ruby on rails 如何在Rails3.x中使用大量可选URL参数构建SEO URL?

Ruby on rails 如何在Rails3.x中使用大量可选URL参数构建SEO URL?,ruby-on-rails,ruby,ruby-on-rails-3,seo,routes,Ruby On Rails,Ruby,Ruby On Rails 3,Seo,Routes,好的,我正在努力使我的URL搜索引擎友好,并从谷歌获得更多的索引功能。基本上,我有一些URL如下所示: resources: :articles do get '(:filter(/:page)', action: :index, on: :collection end /文章?第2页&过滤器=全部 我希望它看起来像这样 /文章/全部/2 我已经让/artiles/:filter/:page部分正常工作,就像我这样做我的路线: resources: :articles do get '

好的,我正在努力使我的URL搜索引擎友好,并从谷歌获得更多的索引功能。基本上,我有一些URL如下所示:

resources: :articles do
  get '(:filter(/:page)', action: :index, on: :collection
end
/文章?第2页&过滤器=全部

我希望它看起来像这样

/文章/全部/2

我已经让/artiles/:filter/:page部分正常工作,就像我这样做我的路线:

resources: :articles do
  get '(:filter(/:page)', action: :index, on: :collection
end
我的问题是如何让页面参数在没有过滤器(或其他可选参数)的情况下工作

/文章/?第页=2

应该像

/第/2条

我一直在考虑使用违禁品,但似乎无法使其发挥作用,类似于此

resources: :articles do
  get ':page', action: :index, on: :collection, constraints: { page: /\d+/ }  
  get '(:filter(/:page)', action: :index, on: :collection
end
编辑

我没有意识到这一点,但上面的方法是有效的,没有的是链接没有生成漂亮的URL。e、 g./articles/all/1,仍在输出/articles?filter=all。这是指向我正在使用的代码的链接:

= link_to "Filter", articles_path(filter: 'all') #=> /articles?filter=all

我想要:/articles/all和/articles/all/2和/articles/2和to all work.

这并不漂亮,但它可能会让你走上正确的方向

module ApplicationHelper
  def articles_with_optional_page_path(params)
    if params[:page] && !!params[:filter]
      articles_with_page_path(params)
    else
      articles_path(params)
    end
  end
end

resources: :articles do
  get ':page', action: :index, on: :collection, constraints: { page: /\d+/ }, as: :articles_with_page
  get '(:filter(/:page)', action: :index, on: :collection, as: :articles
end
为什么不在URL中添加“页面”一词,以便路由助手知道它正在调用页面,而不是传递过滤器名称:

/articles/page/1
在routes.rb中

resources :articles do
  get 'page/:page', action: "index", on: :collection, constraints: { page: /\d+/ }, as: :articles_with_page
  get ':filter/page/:page', action: :index, on: :collection, as: :articles
end 
括号表示路由名称的该部分是可选的。因为在你的情况下,你是非常具体的模式,它应该匹配,它会删除它们

此外,您还调用了过滤器名称空间
:articles
——这将与默认索引路由冲突,因此您应该重命名它或将
传递给资源块,除了:[:index]

resources except: [:index] do
  # stuff here
end

希望这能有所帮助

创建“articles/2/all”并使过滤器成为可选的不是更容易吗?我需要两种方式(/articles/all和/articles/2和/articles/all/2)但它实际上是按照我的设置方式工作的,实际上,我只需要配置链接以创建正确的url。这有助于使用as:命名路线,但我的kaminari链接没有按照我希望的方式工作。只是做了一些小调整,效果很好,已经在google中为所有缺失的页面编制了索引。谢谢