Is there a way to generate a scaffold in rails 3.0 so that scaffold.css does NOT get created? Something at the command line I can enter to skip that step?
Thanks
There is a --no-stylesheets flag you can use:
rails g scaffold MyModel --no-stylesheets
You can also disable it by default -- in config/application.rb:
config.generators do |g|
g.stylesheets false
end
Rails itself only uses it for scaffold.css AFAIK, but unfortunately the same hook could be used by other generators, so you might need to remember to pass --stylesheets for a third-party gem that generates assets, for instance. It'd be really nice if Rails had an explicit option for scaffold.css :-/
You can find other generator options in the Rails Guides, by the way. Helpers are nice to turn off by default and generate them when you actually want them.
Since Rails 5.0, there is a configuration in config/application.rb which specifically disables generating the app/assets/stylesheets/scaffolds.css, but still generates the stylesheets for your new resource:
config.generators do |g|
g.scaffold_stylesheet false
end
You can also pass it in as the --no-scaffold-stylesheet command line option:
rails generate scaffold post title body:text --no-scaffold-stylesheet
Related
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?
I can't find an option anywhere and the code here doesn't show me any pointers.
We are likely going to be building our own controller / resource generators at some point anyway, for our API, but I was curious if there was a way to turn off this annoyance in the meantime.
It's not a well documented feature, but try to add --skip-template-engine (alias --no-template-engine) option to the command.
rails generate controller foo bar --skip-template-engine
demo on a dummy app:
rails g controller my_controller index show --no-template-engine
create app/controllers/my_controller_controller.rb
route get "my_controller/show"
route get "my_controller/index"
invoke test_unit
create test/functional/my_controller_controller_test.rb
invoke helper
create app/helpers/my_controller_helper.rb
invoke test_unit
create test/unit/helpers/my_controller_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/my_controller.js.coffee
invoke scss
create app/assets/stylesheets/my_controller.css.scss
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.
Just thought I'd try underscoring the --skip-template-engine flag to see if it worked in a generator and it worked a charm! No view templates generated from a bin/rails g controller command in a Rails 4.2 application.
Try:
config.generators do |g|
g.template_engine false
end
A little late I know but these things stick around in Google! ;)
If you're creating an API with no front end, you can go ahead and use rails new --api. However, I don't recommend this option if you do plan to create a front end (for example a single page app) because it turns a lot of things off, including the asset pipeline.
I'm trying to get custom scaffolding working from my engine.
I followed some tutorial on customizing Rails 3.2 scaffolding in a normal Rails App and put my customized templates in the engines /lib/templates/erb/scaffold directory but they don't get picked up by the app that includes the engine. Any suggestions?
Update:
I also tried to override the Rails ScaffoldGenerator's source_path and tried some other paths to put my template in, like:
lib/rails/generators/erb/scaffold/templates
zarazan's answer got me most of the way there, but there are a couple of things wrong with it. Here's what worked for me:
class Engine < Rails::Engine
config.generators do |g|
g.templates.unshift File::expand_path('../../templates', __FILE__)
end
end
Note that this goes in the generators section, not app_generators, and that the path is slightly different.
Also, I think the correct path to store your templates is lib/templates/erb/scaffold, optionally replacing erb with whatever language you are using (like haml or slim.) I know this works for slim. The file names are {_form,edit,index,new,show}.html.erb.
In the file that you declare your engine use this command:
class Engine < Rails::Engine
config.app_generators do |g|
g.templates.unshift File::expand_path('../templates', __FILE__)
end
end
It should shift the preference of what template folder Rails uses by default.
Now just put the template files in lib/templates/erb/scaffold/template_name.erb
Where template_name is one of the following: _form.html.erb, edit.html.erb, index.html.erb, new.html.erb, show.html.erb
Once you include the gem you should be able to use the rails generate scaffold command as normal.
Here is an example of an engine that overrides the default scaffolding in rails:
https://github.com/brocktoncg/gemboree
This is where the template directory is located:
https://github.com/brocktoncg/gemboree/tree/master/lib/templates/erb/scaffold
Are you talking about a controller template? Than you are using the wrong directory. Save your template at
lib/templates/rails/scaffold_controller/controller.rb
Have a look at http://xyzpub.com/en/ruby-on-rails/3.2/templates.html for an example.
I've created a generator for a controller in rails 3.
Now I want to use this generator as the default generator when using the scaffolding generator.
Is that possible?
The correct position for your customized controller file is lib/templates/rails/scaffold_controller/controller.rb
If you simply want to use your own controller template, you can just put it in lib/templates/rails/scaffold_controller/controller.rb
If you want to replace the scaffold_controller_generator code itself, for example, so that the controller scaffold generates additional class files. you can create lib/generators/rails/my_scaffold_controller/my_scaffold_controller_generator.rb with templates under lib/generators/rails/my_scaffold_controller/templates.
Remember to point rails at your new scaffold_controller in config/application.rb:
config.generators do |g|
g.scaffold_controller = "my_scaffold_controller"
end
For my_scaffold_controller_generator.rb you could copy from the railties gem under railties-3.x.x/lib/rails/generators/rails/scaffold_controller if you want to modify default behaviour, or inherit from it if you just want to add functionality:
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
module Rails
module Generators
class MyScaffoldControllerGenerator < ScaffoldControllerGenerator
source_root File.expand_path("../templates", __FILE__)
def new_funtionality
end
end
end
end
You can override the templates that Rails uses for its generators. In this instance, just place the file at lib/templates/scaffold_controller/controller.rb and modify it how you wish. The next time you run rails g scaffold [modelName] it will pick up this new controller template and use it.
This is covered in Section 6 of the Creating and Customizing Rails Generators official guide.
This seems to have changed slightly with Rails 4. You can see which template the generator will look for in the invoke line when the scaffold is generated, and your template folder name should match this:
rails generate scaffold blub
...
invoke responders_controller
If you're using rails g scaffold_controller blubs the location of the template should be:
lib/templates/rails/scaffold_controller/controller.rb
If you're using rails g scaffold blub the location of the template should be:
lib/templates/rails/responders_controller/controller.rb
If anyone is wondering why this isn't working in a default Rails 4 install, it's because jbuilder is inserting itself into the template path before the override location. I don't need jbuilder so I removed it, but I also reported an issue in Github. Hopefully it'll be fixed soon.
I have removed my DataMapper specific models and gems in my Rakefile and removed all databases. I also updated my database.yml file. Now, when I attempt to generate a model with
rails g model Car year:integer make:string model:string
I get:
No value provided for required options '--orm'
Is there someplace that I am missing the specification of Active Record? I've been unable to find any documentation for switching an application's ORM.
Have a look in config/application.rb, you may have a line that looks like this:
config.generators do |g|
g.orm :datamapper
end
Change that :datamapper symbol to :active_record or remove that line completely to switch back to ActiveRecord.
If it's not there, you may have a file in config/initializers which does this setup for you.
Is there a way to do this from the application.rb?
edit: short answer on top
If you want to do this for every time you run the generators, you do indeed customize it within your application.rb file. Put this code in the file inside the Application class definition.
config.generators do |g|
g.view_specs false
g.helper_specs false
end
You can also accomplish this by passing some options into the generator command. This Railscast goes over the process in more detail, but the basic idea is pretty simple.
Rails generators can take several options. You can see the options for the controller generator by running rails g controller -h. Assuming you have Rspec set up already, if you look at the output, you notice a section that says "Rspec options". It looks like this:
Rspec options:
[--controller-specs] # Indicates when to generate controller specs
# Default: true
[--view-specs] # Indicates when to generate view specs
# Default: true
To negate these boolean values, just pass them in with a "no" in front of the name. So if you wanted a controller with no specs for your view, you would call it like this:
rails g controller Foobar index show new create --no-view-specs
And you would get a controller with the correct views and actions created for you, but no specs for your views.
The same thing applies if you are using the scaffold generator. There is a --helper-specs option, so if you wanted no view or helper specs you would run:
rails g scaffold Foobar name:string --no-helper-specs --no-view-specs