13

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 assets for every controller, I put following codes into config/application.rb:

config.generators do |g|
  g.helper false
  g.assets false
end

There is another thing I don't want to happen. The generator adds a line get "foo/index" to my config/routes.rb. How can I prevent it?

Tsutomu
  • 4,848
  • 1
  • 46
  • 68

7 Answers7

16

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

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
4

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.

Fivell
  • 11,829
  • 3
  • 61
  • 99
  • Thanks. It seems that there is no easy way to disable this feature of controller generator. It will be enough for me to confirm this fact... – Tsutomu Jan 08 '14 at 10:10
  • This is no longer a correct answer. `skip_routes true` is the correct solution as of Rails 5 – ekampp Mar 09 '16 at 19:17
3

untested...

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

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

Mr. Ronald
  • 687
  • 4
  • 13
  • 1
    Mr. Ronald, unfortunately, your suggestion does not change the behavior of controller generator. Tested on Rails 4.1.0.beta1. Thanks, anyway. – Tsutomu Jan 01 '14 at 10:26
  • This should be the code: https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/assets/assets_generator.rb => DOES include options. https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb => DOES NOT include options :-( – Mr. Ronald Jan 02 '14 at 15:16
  • It seems that `g.resource_route` controls the behavior of resource generator, but does not change that of controller generator. – Tsutomu Jan 03 '14 at 04:46
  • Agree with your comment above that it doesn't look like you can stop the controller generator from generating a route (unless you don't pass it any actions - it appears if there are no actions passed no route is generated). You could also write a template to run after you generate a controller that used `gsub_file` to remove the offending routes. But I would agree if you felt that all of these solutions sound like more work than just deleting the routes from routes.rb. – Steve Rowley Jan 05 '14 at 21:08
3

If you want to avoid assets or helpers for all controllers, then you can write the following lines in application.rb

config.generators.stylesheets = false
config.generators.javascripts = false
config.generators.helper = false

But if want to avoid for any 1 controller then you generate controller like this

rails g controller test --no_assets
rails g controller test --no_helper
rails g controller test --no_javascripts
rails g controller test --no_stylesheets
Shanky Munjal
  • 671
  • 5
  • 18
3

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

Siva
  • 7,780
  • 6
  • 47
  • 54
2

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

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

Create your own generator! Following link will help:

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

omarvelous
  • 2,774
  • 14
  • 18