【发布时间】:2018-03-10 17:30:20
【问题描述】:
我正在尝试从显示操作重定向到自定义收集操作,但 id 参数被转移,导致路由失败。一个最小的例子:
routes.rb:
resources :first_models, only: [:show]
resources :second_models do
get 'custom_action', on: :collection
end
first_models_controller.rb
class FirstModelsController < ApplicationController
def show
redirect_to controller: 'SecondModelsController', action: 'custom_action'
end
end
second_models_controller.rb
class SecondModelsController < ApplicationController
def custom_action
# Do something
end
end
设置完成后,导航到 /first_models/2 会导致错误:
No route matches {:action=>"custom_action", :controller=>"SecondModelsController", :id=>"2"}
我不知道如何从原始请求中去除 id 参数,以便路由匹配。
【问题讨论】:
标签: ruby-on-rails activerecord url-routing