#148
Feb 09, 2009

App Templates in Rails 2.3

In Rails 2.3 you can provide a template when generating a new Rails application. See how in this episode.
Tags: rails-2.3
Download (25.8 MB, 11:36)
alternative download for iPod & Apple TV (16.9 MB, 11:36)

Resources

sudo gem install rails --source http://gems.rubyonrails.org
rails -v
rails store -m base_template.rb
rails store -m auth_template.rb
# base_template.rb
run "echo TODO > README"

generate :nifty_layout

gem 'RedCloth', :lib => 'redcloth'
gem 'mislav-will_paginate', :lib => 'will_paginate', :source => 'http://gems.github.com'
rake "gems:install"

if yes?("Do you want to use RSpec?")
  plugin "rspec", :git => "git://github.com/dchelimsky/rspec.git"
  plugin "rspec-rails", :git => "git://github.com/dchelimsky/rspec-rails.git"
  generate :rspec
end

git :init

file ".gitignore", <<-END
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
END

run "touch tmp/.gitignore log/.gitignore vendor/.gitignore"
run "cp config/database.yml config/example_database.yml"

git :add => ".", :commit => "-m 'initial commit'"

# auth_template.rb
load_template "/Users/rbates/code/base_template.rb"

name = ask("What do you want a user to be called?")
generate :nifty_authentication, name
rake "db:migrate"

git :add => ".", :commit => "-m 'adding authentication'"

generate :controller, "welcome index"
route "map.root :controller => 'welcome'"
git :rm => "public/index.html"

git :add => ".", :commit => "-m 'adding welcome controller'"

RSS Feed for Episode Comments 31 comments

1. Jamie Appleseed Feb 09, 2009 at 00:24

This might just turn out to be insanely useful. The other day I started a fresh Rails app and realized just how many settings I were fiddling around with.. didn't feel very Rails like.

Scripts on Github, writing a function in the bash file.. great advice.


2. Tex Feb 09, 2009 at 01:06

Many thanks Ryan !!!!


3. QuBiT Feb 09, 2009 at 01:24

I'm really impressed by the possibilities which these templates are providing to us.

I agree with Jamie, because customizing the app after running the rails command takes sometimes very long and could be now easily speedup.

great screencast!


4. jeroen Feb 09, 2009 at 01:39

I don't know, I just don't find this any more useful than keeping "templates" in Subversion (or git).

I have several project templates under version control and clone new projects off those.


5. Andy Pflueger Feb 09, 2009 at 06:16

For some reason, the MOV file stops halfway through for me. Anybody else have that problem?


6. Michelangelo Feb 09, 2009 at 06:38

Hello Ryan,
Ruby's output delimiter %q provides a concise alternative of here documents, especially when you have a few lines of code. You can see an example here:

http://gist.github.com/60801

Thanks again!


7. Stephen Celis Feb 09, 2009 at 07:17

Michelangelo, you'd save another character if you dropped the "q" ;)

Heredocs can offer a different kind of concision, though: http://gist.github.com/60823


8. Yitzhak Bar Geva Feb 09, 2009 at 09:44

I also experienced the movie getting cut off in the middle, at the height of the suspense. I was sooo engrossed. Ryan, Great movie. Please, can't we see the rest?


9. Carl Feb 09, 2009 at 11:14

Awesome screencast Ryan! I'm really glad you did this one now since I can see some great uses for app templates. I didn't have any problems with the video on Linux using mplayer.


10. iGEL Feb 09, 2009 at 13:03

@jeroen: One disadvantage of your solution is, that your "templates" do include one specific version of rails and included gems & plugins (Updating rails doesn't update all files in your app. E.g. the config/environment.rb or the config/initializers/* are only generated for new apps).

Also you can't use functions like ask or yes? nor can your "templates" inherit from other templates.


11. Ryan Bates Feb 09, 2009 at 13:53

@Andy, @Yitzhak interesting, it is downloading all the way for me. Try right-clicking the link and choosing download instead of having it load in the browser. You may also want to try the alternative format (for iPod).


12. Robert Matei Feb 10, 2009 at 01:56

Curious about applying templates to existing apps, as I'll be doing it a lot. Will everything behave gracefully?


13. cbmeeks Feb 10, 2009 at 19:59

Using the following
http://pastie.org/385727

git :commit is executed before git :add.

Anyone know why? I'll try to break it into two lines but I just thought it was weird.


14. Christoph Olszowka Feb 11, 2009 at 06:40

@cbmeeks: This is probably because Ruby < 1.9 does not keep insertion order on hashes:

irb(main):002:0> {:add => ".", :commit => "-m 'foo'"}
=> {:commit=>"-m 'foo'", :add=>"."}

Thus, the commit command will be executed first, even though you gave it as the "second" argument. I don't know about the inner workings of the git method in the Rails Templates, but probably it's just a plain "def git(options)", so the arguments you provide become a plain hash with mentioned problem.

@Ryan: I think there is an issue with using both git commit and rake gems:install, since the former will use your git credentials specified in your user config, while the latter will need sudo rights.

Executing the whole script as sudo will let gems install, but will probably give you empty git committer names (as long as you do not have them specified for root as well) and of course root permissions on all your apps files.

I found a nice detail in Peter Coopers example template (see http://gist.github.com/33337), which executes gems:install with the additional :sudo option:

rake('gems:install', :sudo => true)

Thanks for another great tutorial Ryan!


15. Deger Feb 11, 2009 at 07:17

I like the new feature, thanks you!


16. Chris Your Feb 11, 2009 at 09:57

If anyone has troubles installing Rails 2.3.0 RC1, you may need to install Rack first:

sudo gem install rack


17. cbmeeks Feb 11, 2009 at 18:56

@Christoph Olszowka

I bet you're right! That makes sense.

I do have Ruby 1.9 installed but I can't seem to get any gems to work with it. I'm running Rails 2.3 just fine. But I had to run it with Thin and not webrick because each request was taking 40 seconds...but I digress... :-)

@RyanB

I had to add "include Authentication" in the application_controller after following your example. No biggie. I might have messed up but anyway, just FYI.

BTW, you rock dude!
~cbmeeks


18. scott Feb 15, 2009 at 11:34

Great screencast, Ryan. Good job.


19. Shubham Feb 15, 2009 at 16:18

Really Nice Article. Great Job.


20. pat cheung Feb 17, 2009 at 01:25

so are these new App Templates going to replace the Base Rails Apps floating around out there (eg bort, blank, etc)?...or does this new feature serve a totally different purpose? Can someone please explain? sorry, for the noob question. thanks!


21. JLWestSr Feb 17, 2009 at 09:13

This is great! Thanks for the heads up on this functionality. This has helped me with opening the flood gates of creative ideas.

Please keep up the great work.


22. danny Feb 27, 2009 at 06:57

I can't seem to get rails to use the template from github.

It just sits at "applying template: http://github..."

and eventually times out. Can anyone help?
Thanks for another great screencast Ryan.


23. Ron Apr 08, 2009 at 15:52

A lot of plugins require you to edit various files for installation. For example, the rails-authorization-plugin requires you to add some stuff to environment.rb.

restful authentication requires you to add include AuthenticatedSystem to your application_controller.

Is there a good way to handle this?


24. StoreCrowd May 19, 2009 at 15:59

We've already found this incredibly useful for reusing applications & other cools things we find in the development process. It allows us to have a working application in minutes that can be tinkered with rather than code it up from scratch.

Almost like a musician having a default musical template that they start with in a sequencer.


25. Jason Schmidt May 21, 2009 at 11:15

I recently made an awesome Rails Template that emulates what Bort and some other people have done. Check it out at:
http://github.com/schmidtjra/Rails-Templates/tree/master


26. Will Clark Aug 26, 2009 at 13:49

@Robert Matei

You can apply templates to existing apps using rake:

rake rails:template LOCATION=/path/to/template.rb


27. Bob Aman Sep 16, 2009 at 21:25

You may want to ignore stuff like .DS_Store in a global .gitignore file rather than your project's .gitignore file simply because there's never a case in any type of project where that should be checked into source control.

Use the global core.excludesfile configuration variable for this, and just set it to ~/.gitignore.


28. nike air Dec 08, 2009 at 06:50

greedy genius http://men.shoes54.com/cat1/Greedy-Genius-for-men.html
creative recreation http://men.shoes54.com/cat1/Creative-Recreation-for-men.html
Ed Hardy for men http://men.shoes54.com/cat1/Ed-Hardy-for-men.html
Ed Hardy for women http://women.shoes54.com/cat1/Ed-Hardy-for-women.html
tk shoes http://women.shoes54.com/cat2/Supra-TK-Society-Terry-Kennedy-Pro-Model.html
creative dicoco http://men.shoes54.com/cat2/Creative-Recreation-Dicoco.html
creative cesario http://women.shoes54.com/cat2/Creative-Recreation-Cesario.html
Timberland for men http://men.shoes54.com/cat1/Timberland-for-men.html


29. cheap adidas shoes Jan 31, 2010 at 19:04

Adidas Shoes Online Shop-Hot Selling Adidas Shoes & Cheap Adidas Shoes.
baye


30. Usb accessories Feb 01, 2010 at 18:53

so are these new App Templates going to replace the Base Rails Apps floating around out there (eg bort, blank, etc)?...or does this new feature serve a totally different purpose? Can someone please explain? sorry, for the noob question. thanks!


31. wholesale electronic Feb 03, 2010 at 06:52

This is great! Thanks for the heads up on this functionality. This has helped me with opening the flood gates of creative ideas.

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