Override default scaffold generator in rails 3 - ruby-on-rails

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.

Related

Custom erb generator called from scaffold

I am looking to not only customise my erb scaffold templates but also to add new templates.
In ScaffoldGenerator < Erb::Generators::Base I can see there there is a way to provide additional templates in %w(index edit show new _form).
So I have created a custom erb generator and templates directory in my application in lib/generators/erb/scaffold/.
However when I run rails g scaffold Something, my custom generator is not picked up by via scaffold_controller. Does this mean I need to provide a custom scaffold generator to use a custom scaffold_controller generator just so it can then use my erb generator?
I can also see the scaffold argument:
ScaffoldController options:
-e, [--template-engine=NAME] # Template engine to be invoked
# Default: erb
Am I able to provide my erb generator as the template engine to be used?
(Using Rails 4)
lib/templates/erb/scaffold is the correct path for rails scaffold.
Just in case...
model goes in /lib/templates/active_record/model/model.rb
controller goes in /lib/templates/rails/scaffold_controller/controller.rb

The class name that Rails generate controller generate

For some reason I can't run a rails generate controller on my machine at the moment, something is messed up but still I need a controller. So decided to create it by hand.
But I can't remember the conventions: when I was saying rails generate controller Therapeutics was it creating a
therapeutics_controller.rb
and a
therapeutics.html.haml
So I can create them by hand?
And what was the class name in the controller? Would it be class TherpeuticsController ?
app/controllers/therapeutics_controller.rb should be
class TherapeuticsController < ApplicationController
# define your actions here..
end
app/views/therapeutics => put in your views here
and that's it. if you need helper or model, simply create it by hand. the rails generators are very convenient, but understanding the conventions and creating it by your own gives you more flexibility.
for better understanding, simple check out the basics http://guides.rubyonrails.org/getting_started.html
Just ran it and this is what I got:
$new_project rails generate controller Therapeutics
create app/controllers/therapeutics_controller.rb
invoke erb
create app/views/therapeutics
invoke helper
create app/helpers/therapeutics_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/therapeutics.js.coffee
invoke scss
create app/assets/stylesheets/therapeutics.css.scss
$new_project

Custom Views Scaffolding in Rails Engines

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.

Rails scaffold without the css file?

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

Prefix for the rails generators

Is there a way to generate a scaffold with the admin folder prefix..For example I want one Admin folder and a few controllers for that admin folder. I want to do a scaffold for each controller in the admin folder and i wanted to know if there was something like
script/generate scaffold admin:something somefield:string
The scaffold generator can take a namespaced argument:
# Rails 3
rails g scaffold Admin::Something somefield:string
# Rails 2
script/generate scaffold Admin::Something somefield:string
Note, that if you'd like to leave model unprefixed, you have to edit some of generated files by hand to implement it. That is why I created rails-admin-scaffold gem and wrote detailed article about custom admin area scaffolding. If that's already not relevant for you, maybe it'll help someone else.

Resources