Url rails 4中的自定义路由不工作
我已经创建了自定义路由以路由到新闻模型Url rails 4中的自定义路由不工作,url,routes,ruby-on-rails-4,Url,Routes,Ruby On Rails 4,我已经创建了自定义路由以路由到新闻模型 resources :news, only: [:index] do collection do get 'page/:page', action: :index end end get "news/:id(/p/:p)", to: 'news#show', as: 'news' 像这样生成url http://localhost:3000/news/4/page/2 index.html.er
resources :news, only: [:index] do
collection do
get 'page/:page', action: :index
end
end
get "news/:id(/p/:p)", to: 'news#show', as: 'news'
像这样生成url
http://localhost:3000/news/4/page/2 index.html.erb is right
和show.html.erb
<%= link_to 'news', news_path(@news, '2')%>
I hope generate url http://localhost:3000/news/4/p/2
but generate http://localhost:3000/news/4?p=2
我希望生成urlhttp://localhost:3000/news/4/p/2
但是产生http://localhost:3000/news/4?p=2
我认为您遇到了路由冲突,请更改此选项:
get "news/:id(/p/:p)", to: 'news#show', as: 'news'
为此:
get "news/:id(/p/:p)", to: 'news#show', as: 'news_page'
以及使用此链接:
<%= link_to 'News', news_page_path(@news, '2')%>
“news/:id(/p/:p)”-括号代表什么?把它们拿出来。在url(/p/:p)可有可无,url或者你可以做一个“rake路由”和post吗?