How to prevent Rails controller generator to modify config/routes.rb

后端 未结 7 1827
Happy的楠姐
Happy的楠姐 2021-01-11 13:27

Sometimes I run a command like rails g controller foo index to generate skeletons for controller and template.

Because I don\'t want to have helpers and

相关标签:
7条回答
  • 2021-01-11 13:55

    As of Rails 4.2, it's possible to disable route generation with the following code in your application.rb:

    config.generators do |g|
      g.skip_routes  true
    end
    

    Source: https://github.com/rails/rails/commit/4b173b8ed90cb409c1cdfb922914b41b5e212cb6

    0 讨论(0)
  • 2021-01-11 13:55

    Looks like routes generation is hardcoded. Have a look at this method https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/controller/controller_generator.rb#L12

    I think, the simplest way is to override with monkey-patch . Something like

    module Rails
      module Generators
        class ControllerGenerator < NamedBase 
          def add_routes
            #do nothing...
          end
        end
      end
    end
    

    you can put it to initializer and try.

    0 讨论(0)
  • 2021-01-11 14:00

    untested...

    config.generators do |g|
      g.resource_route false
    end
    

    https://github.com/rails/rails/blob/master/railties/lib/rails/generators.rb

    0 讨论(0)
  • 2021-01-11 14:02

    Create your own generator! Following link will help:

    http://guides.rubyonrails.org/generators.html

    0 讨论(0)
  • 2021-01-11 14:02

    This is counter intuitive, but here it is what you're looking for:

    config.generators do |g|
      g.skip_routes true
    end
    
    0 讨论(0)
  • 2021-01-11 14:08

    Since you want this particular application to not generate routes.

    You can deploy your gems to local/project folder and override them.

    In your rails project folder

    bundle install --path /my_rails_path/lib/
    

    Now you can see all of you libraries ported to your project lib/ folder

    Go to the below file (path changes depending upon your versions)

    lib/ruby/1.9.1/gems/railties-3.2.15/lib/rails/generators/rails/controller/controller_generator.rb

    and comment the function add_routes

      def add_routes
        #actions.reverse.each do |action|
        #  route %{get "#{file_name}/#{action}"}
        #end
      end
    

    NOTE: This trick will not affect any other rails application in your system

    0 讨论(0)
提交回复
热议问题