Don't create view folder on rails generate controller

后端 未结 4 930
情歌与酒
情歌与酒 2021-01-31 08:56

Is there a way with the usual generators config to turn OFF the creation of the view folders and action templates when you run a rails generate controller?

4条回答
  •  一生所求
    2021-01-31 09:54

    To skip views from being generated with your controller, disable the template engine.

    Once:

    rails g controller ControllerName action1 action2 --skip-template-engine
    

    Note that every --skip option also has an aliased --no option.

    Default:

    # config/application.rb
    
    config.generators do |g|
      g.template_engine false
    end
    
    # OR
    
    config.generators.template_engine = false
    

    If you have an API-only application (no front end), you may also want to skip assets and helpers from being generated with your controllers.

    Once:

    rails g controller api/users --no-helper --no-assets --no-template-engine
    

    Default:

    # config/application.rb
    
    config.generators do |g|
      g.assets false
      g.helper false
      g.template_engine false
    end
    
    # OR
    
    config.generators.assets = false
    config.generators.helper = false    
    config.generators.template_engine = false
    

    Disabling assets skips stylesheets and javascripts from being generated. If you only want to skip one, use --no-stylesheets or --no-javascripts, or in config/application.rb use:

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

    If your default configuration skips something from being generated (e.g. assets and helpers) but you need them in one case, you can generate them like so:

    rails g controller foo --helper --assets --skip
    

    where --skip skips generating files that already exist.

提交回复
热议问题