【问题标题】:rename rails route重命名 Rails 路线
【发布时间】:2020-11-20 20:49:34
【问题描述】:

我有一个使用 CcpaAcknowledgmentsController 的路由/路径

/ccpa_acknowledgments

我希望路由是,但是我仍然希望它使用 CcpaAcknowledgmentsController

/customers/ccpa_acknowledgments

既然我有这两条路线...

resources :customers

resources :ccpa_acknowledgments

match '/customers/ccpa_acknowledgments', to: 'ccpa_acknowledgments#index', via: [:get]

我不断收到冲突说明 CustomersController 中的 NoMethodError。

有没有办法在不将方法/代码放入 CustomersController 的情况下获得我想要的路线?

【问题讨论】:

  • get '/customers/ccpa_acknowledgments', to: 'ccpa_acknowledgments#index'
  • @IvanC 这给了我上面所说的错误。我尝试使用客户控制器

标签: ruby-on-rails ruby routes


【解决方案1】:

这是这样做的方法

resources :customers do
  get :ccpa_acknowledgments, to: 'ccpa_acknowledgments#index', on: :collection
end

在客户块内部有两个原因:

  • 我们对路径的开头很好/customers
  • 我们不想弄乱其他客户的路线。通过这种方式,您在街区内的路线位于客户默认路线之前,并且在您以 ccpa_acknowledgments 为 id 调用 customers/:id 时不会被看到,因为 rails 会为您在演出前定义该路线负责

然后

get :ccpa_acknowledgments

因为我们需要路径的第二部分/ccpa_acknowledgments

to: 'ccpa_acknowledgments#index'

我们想要指定控制器和操作对,因为我们想要使用 CcpaAcknowledgmentsController,即使我们在客户块内

on: :collection

因为我们不希望在我们的路线中有任何:id。这是在客户集合上定义的路由

按照评论中的要求使用resources 替代。试试

scope :customers do
  resources :ccpa_acknowledgments, only: :index 
end

但你需要把它放在resources :customers之前

【讨论】:

  • 有什么方法可以在不使用“get”的情况下执行此操作并执行资源或其他操作吗?
  • 我已将其添加到我的答案中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
相关资源
最近更新 更多