Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 资源中具有相同块的Rails路由_Ruby On Rails_Rails Routing - Fatal编程技术网

Ruby on rails 资源中具有相同块的Rails路由

Ruby on rails 资源中具有相同块的Rails路由,ruby-on-rails,rails-routing,Ruby On Rails,Rails Routing,我有这样的路线: resources :placements, only: :index do collection do put :update_all delete :destroy_all end end resources :designs, only: :index do collection do put :update_all delete :destroy_all end end #

我有这样的路线:

resources :placements, only: :index do
    collection do
        put :update_all
        delete :destroy_all
    end
end
resources :designs, only: :index do
    collection do
        put :update_all
        delete :destroy_all
    end
end
# etc...
有太多相同的代码,看起来很难看。有没有办法得到这样的东西:

with_options only: :index, collection: { update_all: :put, destroy_all: :delete } do
    resources :placements
    resources :designs
end
提前感谢!:)

附:这段代码看起来不错,但不起作用:(

Rails 4提供了可用于执行此操作的功能:

concern :standard_routing do
  collection do
    put :update_all
    delete :destroy_all
  end
end

resources :placements, only: :index, concerns: :standard_routing
resources :designs,    only: :index, concerns: :standard_routing
这就是说,路由DSL只是Ruby代码,
do
块就是…就是块!因此,即使没有任何特殊的支持,您也可以将其打包成一个很好的简单生成器,或者重用程序

def standard_actions_for(*resources)
  Array(resources).each do |resource|
    self.resources resource, only: :index do
      collection do
        put :update_all
        delete :destroy_all
      end
    end
  end
end

standard_actions_for :placements, :designs
或:


routes文件只是Ruby代码,所以您应该能够通过定义一个带有_options函数的
来完成它,该函数可以做出必要的安排。当块产生时,您需要处理“resources”函数调用。谢谢!我知道这只是DSL,但我尝试找到标准Rails工具。您可以帮助我)
standard_routing = Proc.new do
  collection do
    put :update_all
    delete :destroy_all
  end
end

resources :placements, only: index, &standard_routing
resources :designs,    only: index, &standard_routing