【问题标题】:Customizing resource routes for every action为每个操作自定义资源路由
【发布时间】:2014-01-01 17:46:55
【问题描述】:

我有一个resources :posts。如何自定义它的以下路径以及使用正常资源路径名称调用它的能力:

URL          Controller action      Helper function
'q'          'posts#index'          posts_path
'q/(:id)'    'posts#show'           post_path(:id)
'ask'        'posts#new'            new_post_path
'q'          'posts#create'         posts_path

这是我尝试过的,但没有达到上述预期结果...

get 'q' => 'posts#index', as: :posts
get 'q/(:id)' => "posts#show", as: :post
get 'ask' => 'posts#new'

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 routes custom-routes


    【解决方案1】:

    您可能会遇到错误,因为您尝试分配已在使用的路由名称。

    resources posts 调用导致postspost 路由的定义。如果您更改 as: .. 子句以引用不同的(未使用的)名称,您将不再收到该错误。

    【讨论】:

      【解决方案2】:

      试试resources :posts path => 'q'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-17
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 2015-11-04
        • 1970-01-01
        • 2014-08-22
        • 1970-01-01
        相关资源
        最近更新 更多