【问题标题】:custom route in rails 4 not workingrails 4中的自定义路线不起作用
【发布时间】:2013-12-16 09:46:42
【问题描述】:

我创建了自定义路由来路由到新闻模型

    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

【问题讨论】:

标签: url routes ruby-on-rails-4


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 2016-01-25
    • 1970-01-01
    相关资源
    最近更新 更多