There are these railscasts.
http://railscasts.com/episodes/218-making-generators-in-rails-3 With this one you find out how to create a stylesheets and scaffold generator.
http://railscasts.com/episodes/216-generators-in-rails-3 With this one you find out how to add some files to modify the scaffolding views.
I want to do a mix of the two. I would like to create a generator that also creates scaffolding views. Kinda like Ryan Bates nifty generators or web_app_theme gem (https://github.com/pilu/web-app-theme). I have been searching for a tutorial or some information to point me in the right direction but I can't find exactly what I'm looking for.
I know I'm close. I already know how to create a generator with Railcast 218 but now, how can I make it create view files too?
I would like to run a command like this...
rails g my_scaffold_generator Post title:string body:text
To generate a scaffold for the post resource, enter the following command: rails generate scaffold Post name:string title:string content:text.
Ruby provides a script called Generator. This script can be used to generate many useful items in Rails.
The generate command in Rails is a powerful tool that you can use to streamline workflow. You can use the generate command in the command line to quickly create boilerplate folders and files for a project.
This may well be too late to help, but as I found this while Googling for the same info...
It seems to me that the best approach, at least for learning the ropes, is to duplicate and then alter the existing scaffold generator.
So the first thing that tripped me up is finding the default templates, which do not live in your rails-3.2.0
directory (or whatever version you are on), but in railties-3.2.0
. So for my RVM-based installation they were at:
/Users/leo/.rvm/gems/ruby-1.9.3-p194@gemset/gems/railties-3.2.0/lib/rails/generators/
[Note: your gems directory could be somewhere else entirely, use $> gem environment
to find your gem paths]
In here is erb/scaffold/templates/
which has the files you'd expect (new.html.erb
, _form.html.erb
etc).
You can copy these files to your app's root, into lib/templates/erb/scaffold/
and they will be used instead of the default ones.
If you want to use these in a custom generator, there are two approaches:
1) Use hook_for
to call the regular ERB scaffold generator from your generator.
2) Move/process the templates inside your own custom generator, using copy_file
and similar methods in Thor to move them into place.
There is a decent Rails Guide on this, although I found I didn't really get it until I started digging around in .../railties-3.2.0/lib/rails/generators/...
and looking at how the defaults are structured.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With