#216
May 31, 2010

Generators in Rails 3

Generators in Rails 3 have been completely rewritten to be more modular. This allows you to customize any generator to fit your specific preferences.
Download (43.3 MB, 11:24)
alternative download for iPod & Apple TV (33.7 MB, 11:24)

Update: Removing the last two lines of application.rb (where we specify the fallback) seems to resolve the overriding template issue mentioned at the end of this episode. (Thanks Jari!)

Resources

rails g
rails g scaffold --help
rails g scaffold project name:string --no-stylesheets
bundle install
rails g scaffold task project_id:integer
rails g scaffold category name:string
# Gemfile
group :test do
  gem "shoulda"
  gem "factory_girl"
end

gem "rails3-generators", :group => :development

# config/application.rb
config.generators do |g|
  g.stylesheets false
  g.test_framework :shoulda
  g.fixture_replacement :factory_girl
end
# ...
require 'rails/generators'
Rails::Generators.fallbacks[:shoulda] = :test_unit

RSS Feed for Episode Comments 24 comments

1. Ryan Jun 01, 2010 at 00:32

Awesome, was playing with this yesterday!

Thanks again!


2. Zolzaya Jun 01, 2010 at 01:02

Rails 3 is very exciting! This is perfect! Thanks so much Ryan.

Zolzaya


3. ma_il Jun 01, 2010 at 01:18

This site positively needs a Flattr button! Want an invite?


4. Giannis Tsagatakis Jun 01, 2010 at 01:42

Before a few days i manage to overwrite the migration template adding collation and encoding info.

I have create a new file here:
"lib/templates/active_record/model/migration.rb" so you may have to adjust the the folder name for the scaffolding to work.

I have found the right folder name for the controller/view scaffolding, but at my village PC so i don't have the right folder name here, sorry ;-). One problem that i have found is that the lis of files that the generator makes is hardcoded. So not an easy way to have an _item.html.erb partial other than to copy and rename the generator inside lib/generators.

Keep your good job Rayan


5. Jari Jokinen Jun 01, 2010 at 03:02

The customized scaffold template is not used because you require 'rails/generators' at the end of the application.rb file.


6. Michael Jun 01, 2010 at 05:17

Thanks again!

Here are some nice resources/blog posts of almost everything with Rails 3 generators

http://www.viget.com/extend/


7. Alfredo Ribeiro Jun 01, 2010 at 06:54

Hey Ryan nice video, I learned some more tips about that now.

About replace scaffolds I think the problem is only the path.
I use here for haml -> /lib/generators/haml/scaffold/templates/index.html.haml and other.
I think you need do the same for erb: /lib/generators/erb/scaffold/templates/index.html.erb

You used lib/templates instead of lib/generators


8. Ian Alexander Wood Jun 01, 2010 at 11:08

Kind of related to the topic of generators, I've been working on a gem for quick project creation from templates (not just Rails, any kind of project) You can check it out (pun fully intended) on github:
http://github.com/i0n/jumpstart


9. Robert Dober Jun 01, 2010 at 12:40

Jari is right. It is easy to fix. Use

 config.generators do |g|
   ...
     g.fallbacks[ :shoulda ] = :test_unit
   end

instead

Great work as usual!


10. Boris Barroso Jun 01, 2010 at 15:53

Alfredo Ribeiro, I have tried the route lib/generators/erb/scaffold/templates/index.html.erb
for erb and it didn't work.


11. Jeff Jun 01, 2010 at 19:31

Great episode. I don't know why, but this one was a stand out for me. I really learned a lot, so thank you.


12. José Valim Jun 01, 2010 at 23:49

Hey Ryan,

Yes, it was a bug. When you required rails/generators in config/application.rb, it was being required too early and then Rails::Generators could not copy part of the configuration set in your application.

This was fixed and the guide updated. However, as David Trasbo pointed above, the best solution is to use config.generators.fallbacks. You usually don't want to require "rails/generators" because it would force generators to be loaded in production (for instance).

Sorry for the issue! :)


13. Nico Jun 02, 2010 at 05:59

Nice screencast! I wonder why you never use haml instead of erb. I switched a long time ago and don't regret it at all. It's so much nicer and cleaner to use haml instead of erb. In the latest version of the gem you can even do multiline commands which was a bit awkward to achieve before. Sorry if this is a bit off topic, it just popped into my mind... ;-)


14. sd7444 Jun 03, 2010 at 09:41

Ryan, I love your screencasts, and they have helped me enormously in getting to grips with Rails more quickly than I otherwise would.

However, I can't help feeling that Rails 3 is still (at least) months away from being a version that can be properly utilised by serious developers. I know your railscasts are meant to be at the forefront of what is best in the ROR community, but clearly (to me) the number of necessary workarounds indicated by your vids - and many others on the same subject (upgrading to Rails 3) - leave me with a no-brainer decision on whether to upgrade production systems to Rails 3 - NFW.


15. jin Jun 03, 2010 at 18:37

thanks


16. fundbuero Jun 04, 2010 at 05:55

I'm surprised at the flexible usage of Rails 3. It's amazing!

Great screencast Ryan! Thanks!


17. Pete Yandell Jun 04, 2010 at 20:09

This inspired me to write a blog post on getting Rails 3, RSpec, and Cucumber all set up and working together:

http://blog.notahat.com/posts/43


18. Pankaj Jun 09, 2010 at 21:57

Custom generators! cool. This will surely add a new dimension of generators to opensource contribution for rails, like gems and plugins. Could'nt imagine Rails could improve more :)


19. Alex McTammany Jun 12, 2010 at 09:34

Great screencast per usual. I've started using my own templates for scaffolding and found it makes life just that much simpler. I was wondering if anyone has tried changing the rails new generator so that each new project creates the same lib/templates directory. Figure this kinda makes your nifty-generators a little outdated.
Cheers.


20. chadoh Jul 01, 2010 at 05:29

This didn't work with the non-standard rails3-generators for me, but putting the line

require 'rails/generators'

into a file (named anything) within config/initializers fixed it. I get errors when it tries to created the helpers and stylesheets for some reason, though (error shoulda [not found] it says, though it finds it for the test).


21. nfl replica jerseys Jul 04, 2010 at 20:03

hey buddy,this is one of the best posts that I’ve ever seen; you may include some more ideas in the same theme. I’m still waiting for some interesting thoughts from your side in your next post.


22. elvankent halı yıkama Jul 22, 2010 at 00:18

very good.


25. buy office 2007 Jul 29, 2010 at 19:16

The article written by you very good, I like it very much. I will keep your new article.


25. Acekard 2i Aug 01, 2010 at 21:00

I like your article.


26. asian escorts indianapolis Aug 02, 2010 at 06:21

<pre>Fantastic write-up you could have composed. I came across the topic worth it to read making with an pleasurable examine.
Below are a few a lot more i consider you'll find intriguing also </pre>
<p><a href="http://www.2hotnsexy.com/Indianapolis/Escort-Agencies/tag/elite-escorts-indianapolis/2/">elite escorts indianapolis</a></p>
<p> <a href="http://www.2hotnsexy.com/Indianapolis/Escort-Agencies/tag/facials/47/">facials</a> </p>


26. asian escorts indianapolis Aug 02, 2010 at 06:24

Fantastic write-up you could have composed. I came across the topic worth it to read making with an pleasurable examine.
Below are a few a lot more i consider you'll find intriguing also
<p> <a href="http://www.2hotnsexy.com/Indianapolis/Escort-Agencies/tag/facials/47/">facials</a> </p>


27. asian escorts indianapolis Aug 02, 2010 at 06:24

Fantastic write-up you could have composed. I came across the topic worth it to read making with an pleasurable examine.
Below are a few a lot more i consider you'll find intriguing also


28. Best Site Aug 09, 2010 at 07:43

Best site is worl. New one rail best way sites in.


31. cheap coogi Aug 09, 2010 at 10:19

Here we have popular Polo T-shirt.


32. UGG Boots on sale Aug 10, 2010 at 18:41

Gooooooooooooooooooood luck ~~!!


33. oppo Aug 15, 2010 at 08:38

This was really useful. Thanks Ryan!


34. Rip Blu-ray for Mac Aug 18, 2010 at 01:33

Thanks,it's so good.
suport!


35. Air Jordan Spizike Aug 19, 2010 at 00:12

This was really useful. Thanks Ryan! Best site is worl. New one rail best way sites in. Great screencast per usual. I've started using my own templates for scaffolding and found it makes life just that much simpler.


36. Анатолий Aug 19, 2010 at 00:16

супер обзор


37. AVI to iPad Aug 19, 2010 at 00:50

my style,like here.


38. wholesale new era hats Aug 20, 2010 at 20:02

I came to your article from another article and am really interested in this learning about this. , I feel strongly about information and love learning more on this. If possible, as you gain expertise, It is extremely helpful for me. would you mind updating your blog with more information?


39. converse all star Aug 20, 2010 at 20:52

love converse all star,love yourself.High quality low price.It's fit for you.


40. tile floor vacuum Aug 24, 2010 at 10:27

I experience some strange behaviours when playing around with this multistep form. Sometimes, when pressing back to change a step, it jumps two steps forward when pressing next afterwards. Somebody else who sees this in his form?


41. clothes store Aug 24, 2010 at 19:13

David Heinemeier Hansson..thanks. [url=http://www.icheapwholesale.com]cheap wholesale clothes[/url]


42. derriopo Aug 26, 2010 at 01:30

Interesting, but as usual..<a href="http://e-spasibo.ru/article_info.php?articles_id=7">Подарок маме</a>


43. Adam Hill Aug 26, 2010 at 03:21

A hint for anyone stuck trying to override the default model when using the scaffold generator: make a template file in lib/templates/active_record/model/model.rb


44. louis vuitton shoes Aug 26, 2010 at 21:18

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


45. mbt shoes sale Aug 28, 2010 at 09:41

If possible, as you gain expertise, It is extremely helpful for me. would you mind updating your blog with more information?


46. cheap mbt shoes Aug 28, 2010 at 10:05

the same theme. I’m still waiting for some interesting thoughts from your side in your next post.


47. cheap ugg boots sale Aug 28, 2010 at 10:14

Great screencast per usual. I've started using my own templates for scaffolding and found it makes life just that much simpler.


48. eztoo Aug 29, 2010 at 18:28

Try our products. There will be surprises.
http://www.eztoosoft.com


49. herve leger dress Aug 30, 2010 at 19:53

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it.


50. herve leger dress Aug 30, 2010 at 19:59

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it.


51. snow boots Aug 30, 2010 at 20:15

I have found the right folder name for the controller/view scaffolding, but at my village PC so i don't have the right folder name here, sorry ;-).


52. louis vuitton sunglasses Sep 01, 2010 at 22:40

Hi, good post, and awesome weblog you have here!
What’s this theme you use? Is it customized?
If customized can you please facilitate me the website of the company who designed it for you?
I would apreciate to have him/her do one for me also!


53. blu ray ripper Sep 01, 2010 at 23:38

This is really a nice guide for Newbies like me. Thank you.

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source