#222 Rack in Rails 3
The router in Rails 3 is fully compatible with Rack. Learn how to embed Sinatra into an app, handle redirects, and improve on Rails Metal.
- Download:
- source codeProject Files in Zip (99.9 KB)
- mp4Full Size H.264 Video (12.9 MB)
- m4vSmaller H.264 Video (8.73 MB)
- webmFull Size VP8 Video (21.4 MB)
- ogvFull Size Theora Video (16.2 MB)
Resources
- Episode 203: Routing in Rails 3
- Episode 150: Rails Metal
- The Powerful New Rails Router
- The Rails 3 Router: Rack It Up
- Generic Actions in Rails 3
- Routing Rails Guide
- Full episode source code
routes.rb
root :to => "home#index"
root :to => HomeController.action(:index)
root :to => HomeApp
match "/about" => redirect("/aboutus")
match "/aboutus" => "info#about"
resources :products
match "/p/:id" => redirect("/products/%{id}")
match "/processes" => ProcessesApp.action(:index)
root :to => "home#index" root :to => HomeController.action(:index) root :to => HomeApp match "/about" => redirect("/aboutus") match "/aboutus" => "info#about" resources :products match "/p/:id" => redirect("/products/%{id}") match "/processes" => ProcessesApp.action(:index)
Gemfile
gem "sinatra"
gem "sinatra"
lib/home_app.rb
class HomeApp < Sinatra::Base
get "/" do
"Hello from Sinatra!"
end
end
class HomeApp < Sinatra::Base get "/" do "Hello from Sinatra!" end end
lib/processes_app.rb
class ProcessesApp < ActionController::Metal
include ActionController::Rendering
append_view_path "#{Rails.root}/app/views"
def index
@processes = `ps -axcr -o "pid,pcpu,pmem,time,comm"`
render
end
end
class ProcessesApp < ActionController::Metal include ActionController::Rendering append_view_path "#{Rails.root}/app/views" def index @processes = `ps -axcr -o "pid,pcpu,pmem,time,comm"` render end end
app/views/processes_app/index.html.erb
<h1>Processes</h1>
<pre><%= @processes %></pre>
<h1>Processes</h1> <pre><%= @processes %></pre>

