有没有办法确保该*_path
方法识别自定义路由?
我有一个资源,以及到同一个控制器的自定义路由:
resources :news, :only => [:index, :show], :path => :nieuws
match '/nieuws/:cat/:id/:slug', :to => 'news#show'
match '/nieuws/:id/:slug', :to => 'news#show'
这工作正常,并且在请求http://www.example.com/nieuws/category/1/slug
时会显示正确的新闻项目。问题是我想使用该news_path
方法链接到我的新闻项目。为了实现这一点,我在我的新闻项目模型中添加了以下代码:
def to_param
if news_category
"#{news_category.slug}/#{id}/#{slug}"
else
"#{id}/#{slug}"
end
end
当不/
用于分离 cat、id 和 slug 时,它可以正常工作,但是当使用/
news_path 方法失败并显示请求的页面时:
No route matches {:action=>"show", :controller=>"news", :id=>#<NewsItem id: 1...etc
我的rake routes
输出:
news_index GET /nieuws(.:format) {:action=>"index", :controller=>"news"}
news GET /nieuws/:id(.:format) {:action=>"show", :controller=>"news"}
/nieuws/:cat/:id/:slug(.:format) {:controller=>"news", :action=>"show"}
/nieuws/:id/:slug(.:format) {:controller=>"news", :action=>"show"}
我已经尝试添加, :id => /[0-9]+\/.+/
到我的资源路由的末尾,这可以使用 a/
但因为它只使用正则表达式我无法:cat
从 URL 获取参数
那么,有没有办法确保我的自定义路由被该news_path
方法识别?