我正在对generator scaffold的默认行为进行一些更改,我想更改路由的格式。
当我们运行rails g scaffold Post title mesage
时,文件routes.rb
返回如下:
resource :posts
但是我想要这个:
resources :posts do
collection do
delete :destroy_selected
end
end
我已经看到这是在文件railties-7.0.4.2/lib/rails/generators/rails/resource_route/resource_route_generator.rb
,但我不知道如何在我的项目中做更改。
10分钟后我做了另一个测试,它工作了!
答案如下:
// lib/generators/rails/scaffold_controller_generator.rb
require 'rails/generators/rails/resource_route/resource_route_generator'
module Rails
module Generators
class ResourceRouteGenerator < NamedBase
def add_resource_route
return if options[:actions].present?
route "resources :#{file_name.pluralize} don collection don delete :destroy_selectedn endnend", namespace: regular_class_path
end
end
end
end