Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Url rails 4中的自定义路由不工作_Url_Routes_Ruby On Rails 4 - Fatal编程技术网

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吗?