#218
Jun 14, 2010

Making Generators in Rails 3

Generators in Rails 3 have been rewritten to use Thor which means the code used to create a generator is quite different. Here you will learn the new way to make generators in Rails 3.
Download (14 MB, 10:07)
alternative download for iPod & Apple TV (13.8 MB, 10:07)

Resources

rails g generator --help
rails g generator layout
rails g layout --help
rails g layout admin
rails g layout foo --skip-stylesheet
# lib/generators/layout/layout_generator.rb
class LayoutGenerator < Rails::Generators::Base
  source_root File.expand_path('../templates', __FILE__)
  argument :layout_name, :type => :string, :default => "application"
  class_option :stylesheet, :type => :boolean, :default => true, :desc => "Include stylesheet file."
  
  def generate_layout
    copy_file "stylesheet.css", "public/stylesheets/#{file_name}.css" if options.stylesheet?
    template "layout.html.erb", "app/views/layouts/#{file_name}.html.erb"
  end
  
  private
  
  def file_name
    layout_name.underscore
  end
end
# lib/generators/layout/templates/layout.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title>Untitled</title>
    <%- if options.stylesheet? -%>
    <%%= stylesheet_link_tag "<%= file_name %>" %>
    <%- end -%>
    <%%= javascript_include_tag :defaults %>
    <%%= csrf_meta_tag %>
    <%%= yield(:head) %>
  </head>
  <body>
    <div id="container">
      <%% flash.each do |name, msg| %>
        <%%= content_tag :div, msg, :id => "flash_#{name}" %>
      <%% end %>
      <%%= yield %>
    </div>
  </body>
</html>

RSS Feed for Episode Comments 15 comments

1. Ryan Jun 14, 2010 at 02:06

Cool, busy updating my gems with rails 3 generators.

Thanks


2. leo Jun 14, 2010 at 03:30

Just reviewed #216, and found this one. You are unbelievable. Thanks a lot.


3. Aaron Jun 14, 2010 at 03:43

Ryan, I don't know what you started doing with your videos, but they are all messed up now when I view them with VLC on Ubuntu.


4. Xpepermint Jun 14, 2010 at 05:28

I believe the problem is in VLC.


5. Aaron Jun 14, 2010 at 06:03

@Xpepermint, well, of course it is but my version VLC hasn't changed, so he must be doing something different with his encoding since about episode 210 give or take an episode. I'm not sure why he changed the way he encoded his video files since then, but I'm just wondering if he could go back to doing what he was doing before this started happening.


6. Jerry Jun 14, 2010 at 06:27

Hey Ryan, great cast as always. Already enjoyed th cast about generators for version 2.x.

Thx fot that one.


7. Chip Castle Jun 14, 2010 at 10:40

Hi Ryan,

I just wanted to say that your weekly screencasts always inspire me to be a better Rails programmer. This week is no exception.

Awesome work!
Chip


8. Michael Jun 14, 2010 at 14:59

Nice one! Thanks lads!


9. Dom Jun 14, 2010 at 17:27

Very cool, thanks once again. I haven't made the switch to Rails 3 yet, but these episodes are enticing me.


10. sekrett Jun 16, 2010 at 00:08

@Aaron, I have no problems on Ubuntu 9.10 with Totem player.


11. Biznes idei Jun 16, 2010 at 10:02

Thanks! I need it. ;)


12. M Jun 16, 2010 at 17:09

Thanks Ryan, again a wonderful cast


13. Fredrik Jun 17, 2010 at 14:28

So much new cool features in Rails 3, creating generators actually seems fun now!:)


14. Benjamin Lewis Jun 17, 2010 at 19:46

Hi Ryan

I just read this "You no longer need to place a minus sign at the end of a ruby interpolation inside an ERb template to remove the trailing carriage return in the HTML output."
http://guides.rails.info/3_0_release_notes.html

Keep the Rails 3 ones coming.
Thanks
Ben


15. Robert Dober Jun 18, 2010 at 11:52

Aaron, same problem here, however I use the alternate download now, the *.m4v that is, and it works fine with VLC.


16. he9lin Jun 18, 2010 at 23:39

Thanks Ryan for another great episode. I've learned a lot here. . (grats lakers repeat :p)


17. Johannes Jun 19, 2010 at 05:19

Nice episode.

Wouldn't a better name for "layout.html.erb" be "layout.html.erb.erb"?


18. strapping machine Jul 08, 2010 at 19:20

Thanks for sharing the information.It is definitely going to help me some time.


19. shrink wrap Jul 08, 2010 at 19:21

Lots of Good information in your posting, I bookmarked your blog post so I can visit again in the near future, Cheers!


20. welding rod Jul 11, 2010 at 17:49

this was a really quality post.I wasn't aware of the many ripples and depth to this story until I surfed here through Google! Great job.


21. welding electrode Jul 11, 2010 at 18:05

There are certainly a lot more details to take into consideration, but thanks for sharing this information.


22. Jordan Shoes Jul 20, 2010 at 02:59

Thank you.I hope I can improve through learning this respect. But overall, it's very nice. Thank you for your share!

Report as Spam


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

Thanks for you


26. beats by dre Jul 26, 2010 at 05:18

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


26. asics shose Jul 27, 2010 at 20:12

It is my pleasure to read this article,I look forward to reading more.


26. ibm thinkpad battery Jul 29, 2010 at 02:14

very useful code to us and I will come here often.


27. chanel bags online Aug 04, 2010 at 02:00

Cool, busy updating my gems with rails 3 generators.

Thanks


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:39

Gooooooooooooooooooood luck ~~!!


33. free directory list Aug 11, 2010 at 22:43

WOW, this is great!


34. Mike Aug 16, 2010 at 18:59

I think you do a great service to the Rails community, and I think that you do an outstanding job on these videos. You've started saying "basically" pretty often lately though.


35. Rip Blu-ray for Mac Aug 18, 2010 at 01:27

Thanks,it's so good.
suport!


36. Wholesale hats Aug 20, 2010 at 19:59

The dissertation chapters should be perfectly composed by distinguished thesis service, when people want to present a writing talent. Thus, this is manifestly that you understand the right way to finish a superb issue referring to this topic. Thank you very much for distributing this.


37. converse all star Aug 20, 2010 at 20:33

love converse all star,love yourself.click here http://www.conversewu.com


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

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


39. medyum Aug 22, 2010 at 09:24

The information you provided was very useful. Because of your help, thank you.


40. medyum Aug 22, 2010 at 09:25

Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming


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

The blog article very surprised to me! Your writing is good. In this I learned a lot! Thank you!


42. clothes store Aug 24, 2010 at 19:11

David Heinemeier Hansson..thanks


43. louis vuitton shoes Aug 26, 2010 at 21:17

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


44. rap Aug 29, 2010 at 08:48

Gooooooooooooooooooood luck ~~!!


45. herve leger dress Aug 30, 2010 at 20:07

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


46. snow boots Aug 30, 2010 at 20:13

Is it possible to just inherit from ActiveModel::Base?


47. Dolce & Gabbana Belts Sep 01, 2010 at 22:47

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!


48. blu ray ripper Sep 01, 2010 at 23:57

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