- Recent
- Popular
- Tags (1)
- Subscribers (8)
- Rails templatesYesterday
-
So now that Edge Rails got templates ( Thanks to Jeremy ) I just wanted to give a top level overview.
Templates are simple ruby files containing DSL for adding plugins/gems/initializers etc. to your freshly created Rails project. To apply the template, you need to provide rails generator with location of the template you wish to apply, using -m option :
rails blog -m ~/template.rbThanks to the magic of open-uri, the template location can be a URL too :
rails blog -m http://gist.github.com/31208.txtA very simple template would look like :
1 2 3 4 5 6 7 8 9 # template.rb run "rm public/index.html" generate(:scaffold, "person name:string") route "map.root :controller => :person" rake("db:migrate") git :init git :add => "." git :commit => "-a -m 'Initial commit'"That’s very self explanatory. Here are the key methods for the te
- Ruby on Rack #2 - The BuilderNovember 17
-
In Ruby on Rack #1 – Hello Rack! we used rackup to make port/server configurable. And rackup’s config file looked like :
1 2 # config.ru run Proc.new {|env| [200, {"Content-Type" => "text/html"}, "Hello Rack!"]}Under the hood, rackup converts your config script to an instance of Rack::Builder.
What is Rack::Builder ?
Rack::Builder implements a small DSL to iteratively construct Rack applications.
Rack::Builder is the thing that glues various Rack middlewares and applications together and convert them into a single entity/rack application. A good analogy is comparing Rack::Builder object with a stack, where at the very bottom is your actual rack application and all middlewares on top of it, and the whole stack itself is a rack application too.
Let’s say our rack application is called infinity :
1 2 infinity = Proc.new {|env| [200, {"Content-Type" => - Ruby on Rack #1 - Hello Rack!November 17
-
Ruby community is coming up with new frameworks almost every week, but in midst of that, Rack isn’t getting enough attention. Attention that it deserves. And also, the next stable release of Rails after 2.2 will have a better public facing interface for taking full advantage of Rack.
Rack was initially inspired from pythons’s wsgi and it quickly became the de-facto web application/server interface in the ruby community, thanks to it’s simplicity and preciseness. You might want to read Introducing Rack from the creator of rack – Christian Neukirchen before reading this post.
What is Rack ?
Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
Practically speaking, you can divide “Rack” in two parts :
Rack Specification
Rack specification sp
- Rails meets Sinatra #2 - Mix n' MatchNovember 16
-
In my previous post, we saw how to mount a sinatra application at a specific location for a regular application. But using Rack::Cascade, we can take that solution to a whole new height. That is :
- Put sinatra code in any of your regular Rails controllers.
- No need to mount at Sinatra at a specific URI.
- Have Sinatra work for any URI, gracefully fallback to Rails if no Sinatra method matches the path.
- Use your models/libraries etc. in both Rails and Sinatra.
So your controllers can look like :
1 2 3 4 5 6 7 8 class HomeController < ApplicationController get '/' do Item.count.to_s end def index end endand even :
1 2 3 4 5 6 7 8 9 class SiteController < ApplicationController get '/:name' do Site.find_by_name(params[:name]).html end def new end endAnd environment.rb:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 RAILS_GEM_VERSION = '2.2.0 - Rails meets SinatraNovember 10
-

For real !
It was pleasantly simple to get Rails + Sinatra run in the same process.
First of all, put your Sinatra application inside RAILS_ROOT. My Sinatra app is called tiny :
1 2 3 4 5 6 7 8 # RAILS_ROOT/tiny.rb require 'sinatra' before { request.env['PATH_INFO'].gsub!(/\/$/, '') } get '' do 'Hello Sinatra!' endAnd then do the rack dance – racked.rb :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # RAILS_ROOT/racked.rb require File.dirname(__FILE__) + '/config/environment' require 'thin' # Sinatra stuff require 'tiny' # Make sinatra play nice set :env, :production disable :run, :reload app = Rack::Builder.new { use Rails::Rack::Static
