rails 4中的自定义路由无法正常工作

时间:2013-11-30 09:24:47

标签: 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'

生成这样的网址

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

1 个答案:

答案 0 :(得分:0)

我认为你有路线冲突,改变这个:

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')%>