如何在不覆盖路径的情况下在资源内创建自定义路由

时间:2016-12-07 22:38:54

标签: ruby-on-rails url routes seo

我在routes.rb

上有这个
:as

它说:

  

无效的路由名称,已在使用中:'resource'您可能使用show选项定义了两个具有相同名称的路由,或者您可能正在覆盖已由具有相同命名的资源定义的路由< / p>

我知道资源会创建两条路径相同的路径,destroyresource_path都使用{{1}},它是如何在内部创建的?以及如何生成我的展示路线而不会覆盖毁灭中的那个?

2 个答案:

答案 0 :(得分:2)

消除不需要的路线的好方法是指定:only选项

resources :user, :only => [:edit] 

而不是

resources :user, :except => [:new, :create, :edit, :update, :show, :destroy]

答案 1 :(得分:0)

在我看来,你可以拿出秀,然后分别定义你想要的路线。看看是否有效:

resources :questions, except: :show

get '/resource/:subject/:id',
  to: 'resource#show',
  as: "resource",  # This is where the error is.
  param: [:name, :id]

编辑:啊,是的。 :as参数需要不同的名称。这将有效:

resources :questions, except: :show

get '/resource/:subject/:id',
  to: 'resource#show',
  as: "resource_show",
  param: [:name, :id]