Rails 3 Routing with Rack
Sun Feb 07 05:56:12 -0800 2010
You probably all have heard that “Rails lets you route to Rack applications directly” and thought “Oh really?” well… bet you didn’t think it would be this simple.
Rails 3 really opens up a whole new world of pluggable awesomeness. This post expects you have already installed Rails 3 —prerelease, if not, follow the instructions in the Release Notes
The goal here is to make a Sinatra app run inside of Rails, taking routes it needs directly using the new Rails routing features.
Code is better at talking than me, so first make a new app:
$ rails app
Let rails do its thing. Then we want to make a simple Sinatra app, let’s make it marginally useful and have it hit Twitter.
Make a directory lib/twitter and in there make a new file called twitter_app.rb, inside of it put:
class TwitterApp < Sinatra::Base
set :root, File.dirname(__FILE__)
get '/twitter' do
@user = 'raasdnil'
t = Twitter::Search.new(@user).fetch
@tweets = t.results
erb :twitter
end
end
This is a basic Sinatra app, it first sets its root directory to be the directory of the current file (needed because using the Rails root will not work) and then responds to one url /twitter that uses the Twitter gem to do the heavy lifting on searching for all the tweets by some weirdo (me).
It then assigns all the tweets to an instance variable and renders the template twitter.erb.
We have to make this template, so create another folder lib/twitter/views and make a file in there called twitter.erb and put in the following:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tweets mentioning <%= @user %></title>
</head>
<style type="text/css" media="screen">
div.tweet { border: 1px solid gray;
height: 50px;
width: 600px;
padding: 5px;
margin-bottom: 10px; }
img.icon { float: left; padding-right: 10px; }
div.text { font-family: verdana, sans-serif; }
div.<%= @user %> { background: #F4F4FF; }
</style>
<body>
<% @tweets.each do |tweet| %>
<div class="tweet <%= tweet.from_user %>" id="<%= tweet.id %>">
<img class="icon" src="<%= tweet.profile_image_url %>" />
<div class="text" >
<%= tweet.text %>
</div>
</div>
<% end %>
</body>
</html>
OK, our Sinatra app is ready to fire. Now hooking it up is simple.
First, we need to make sure we require the needed gems, so open up your Gemfile in Rails root and add the following:
## Bundle the gems you use: gem "sinatra", "0.9.2" gem "twitter", "0.8.3"
Then do a bundle check to make sure we have the gems we need:
$ bundle check The Gemfile's dependencies are satisfied
All good (if that gave you an error, just run bundle install).
Now the final step is to wire up the Rails Router.
Open up config/routes.rb and add a require for your twitter app as well as a route to match ‘/twitter’ and send to your Sinatra App, something like this:
require 'twitter/twitter_app' RackTest::Application.routes.draw do |map| match '/twitter', :to => TwitterApp end
And that’s it!
Fire up your Rails app with rails s as you normally would, and browse to http://127.0.0.1:3000/twitter and proove it to yourself… this stuff works.
Almost too simple.
blogLater
Mikel




Sun Feb 07 09:39:54 -0800 2010
Why lib/twitter.rb? I’ve been placing Sinatra apps in app/metal in Rails 2.
Also, I’m just learning HTML5 but can’t that xmlns attribute be removed? Looks like you’re mixing the HTML5 DOCTYPE with XHTML stiff.
Sun Feb 07 10:05:26 -0800 2010
Awesome! Thanks!
Sun Feb 07 11:21:29 -0800 2010
The xmlns attribute is indeed a remnant of XHTML, but it’s not incorrect to include it: http://diveintohtml5.org/semantics.html
Sun Feb 07 19:46:16 -0800 2010
Thanks Matt, handled it.
Sun Feb 07 19:47:20 -0800 2010
@dan because sinatra is not Rails metal and this shows an example of using a separate application within your rails app, as it is not part of your app, it makes sense for it to be in lib.
The idea being, you could put this app in any rails app and with two lines of code include it.
Mikel
Tue Feb 09 19:01:36 -0800 2010
Definitely too simple. Thanks.
Sat Feb 20 06:32:00 -0800 2010
Shouldnt the sinatra app be get “/” instead of get “/twitter”?
Tue Feb 23 17:50:31 -0800 2010
All kinds of and everything about metal rack,Thank you for visiting our site.http://www.metaldisplayrack.com/sheet-metal_rack.htm
Wed Mar 24 22:25:03 -0700 2010
Lol…that poor spammer in the previous comment thought you were talking about actual sheet metal!
Tue Nov 08 19:46:32 -0800 2011
This was a fantastic post. Really loved reading your weblog post. The information was very informative and helpful.
Sat Apr 24 17:04:49 -0700 2010
@Brian, nice…. I think I will leave it there for prosperity / stupidity value :)
Thu Jul 01 19:09:17 -0700 2010
Thanks for the informative post. I’ve gotten almost everything working nicely but I can’t figure out how to serve static content. I have stylesheet.css in the public folder and I’m not sure how to access it when running the Sinatra app inside a Rails 3 app. Any ideas?
Sun Jan 16 02:14:09 -0800 2011
@Adam
Couldn’t you do something like:
dir = File.dirname(File.expand_path(FILE))
set :public, “#{dir}/public”
Mon Nov 14 03:43:59 -0800 2011
Your post is really good and informative. I’m surprised that your post has not gotten any good quality, genuine comments. You have done a great job by posting this article.
Mon Nov 14 03:45:24 -0800 2011
I’ve gotten almost everything working nicely but I can’t figure out how to serve static content. Thanks for the informative post.
Thu Aug 04 01:39:45 -0700 2011
The person who shaped this post is a genius and knows how to keep the readers joined.Thanks for giving out this with us. I found it informative and interesting.
moulin farine
Mon Nov 21 04:42:58 -0800 2011
The scoop here is to makes a Sinatra app run inside of Rails, transference path it needs directly using the new Rails routing features.
Mon Dec 12 00:51:07 -0800 2011
Very informative and inspiring. Thanks for sharing.
Mon Aug 08 01:16:19 -0700 2011 I’ve gotten almost everything working nicely but I can’t figure out how to serve static content. Thanks for the informative post.
Mon Aug 08 20:18:28 -0700 2011
hey thanks for throwin up this quick guide. I have been STRUGGLING with my rails app, and this really saved me some time. Thanks again.
Tue Aug 23 19:49:50 -0700 2011
Nice! Really helpful post!
Rails 3 is really awesome!
Sat Sep 24 16:34:27 -0700 2011
I love that “Rails lets you route to Rack applications directly”! This has simplified my job so much! It is amazing the time savings that this has given me. It has been so simple to implement and set up using cloud computing. It has been a great way to create and use new plugins. I’m loving everything about it!
Mon Nov 28 11:50:18 -0800 2011
class TwitterApp < Sinatra::Base
get ‘/twitter’ do @user = ‘raasdnil’ t = Twitter::Search.new(@user).fetch @tweets = t.results erb :twitter end"set :root, File.dirname(FILE)
end"
I am having problems witha tha piece of code, the basic Sinatra app, are you guys facing problems or can you run it well?
Fri Oct 21 00:10:44 -0700 2011
This was a fantastic post. Really loved reading your weblog post. The information was very informative and helpful.
Thu Oct 27 02:48:15 -0700 2011
this is really nice post and i like this post
Fri Oct 28 03:00:13 -0700 2011
You probably all have heard that “Rails lets you route to Rack applications directly” and thought “Oh really?” well… bet you didn’t think it would be this simple.
Tue Dec 27 18:30:23 -0800 2011
I’ve gotten almost everything working nicely but I can’t figure out how to serve static content. I have stylesheet.css in the public folder and I’m not sure how to access it when running the Sinatra app inside a Rails 3 app.
Wed Dec 28 00:32:15 -0800 2011
Thanks for sharing this great article ! I feel strongly about it and love learning more on this topic. It is extremely helpful for me. I hope you post again soon
Tue Jan 24 01:20:32 -0800 2012
This was a fantastic post. Really loved reading your weblog post. The information was very informative and helpful.
Tue Jan 24 01:21:04 -0800 2012
This was a fantastic post. Really loved reading your weblog post. The information was very informative and helpful.
Tue Jan 31 04:15:29 -0800 2012
The best person to give you medical advice about liver disease is your doctor. Best thing we can do is recommend perhaps a good doctor if you need a second or third opinion. casino
Thu Feb 02 21:11:25 -0800 2012
Rails 3 real opens up a full new class of pluggable awesomeness. This transfer expects you individual already installed Rails 3 -prerelease, if not, simulate the instructions in the Announcement Notes
Sun Feb 05 20:32:51 -0800 2012
Thanks for the informative post. I’ve gotten almost everything working how much does it cost to build a house nicely but I can’t figure out how to serve static content. I have stylesheet.css in the public folder and I’m not sure How to Select House Plans how to access it when running the Sinatra app inside a Rails 3 app. Any ideas?
Thu Feb 09 02:05:24 -0800 2012
This is a fantastic website and I can not recommend you guys enough. Full of useful resource and great layout very easy on the eyes. Please do keep up this great work. new to dvd
Wed Feb 15 13:07:47 -0800 2012
I still find however that most teams find it difficult to estimate in pure points so to start, it is a good idea to equate 1 SP to 1 Ideal hour (or something like that) but once you get the hang of it you really should try your best to drop the time association. It actually does get easy to do especially if binary trading
Thu Feb 16 04:17:22 -0800 2012
very nice effort…….
thanks for sharing………
Peko drying cabinets
Thu Feb 16 12:37:49 -0800 2012
I remember when I was a service crew in a donut shop, I dream of having my own franchise. But with this price my dreams will stay as a dream. Hope I can save more money to get one. academic writing help
Sat Feb 18 02:11:47 -0800 2012
Rail 3 is an awesome platform and its very intuitive пошив одежды киев
Wed Mar 21 06:05:20 -0700 2012
Faced problems before buy piece of cake after reading your post. You made it look pretty simple. Thanks a lot
Fri Mar 23 06:55:25 -0700 2012
Rails in a Nutshell is a concise introduction to Rails, an overview of commands and configurations, and a guide to the parts of Rails you’ll be using every day.Full of examples and explanations, this book kicks your skills into high-gear by showing you how to take advantage of the Model-View-Controller concept with tiny but expressive bits of Ruby that power some of the world’s biggest and fastest web services.
Mon Mar 26 02:19:39 -0700 2012
I remember when I was a service team in a donut shop, I dream of having my own franchise. But with this price my dreams will stay as a dream. Hope I can save more beans to get one. easter facebook status
Mon Mar 26 14:20:27 -0700 2012
This one is very nicely written and it contains many useful facts. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement. Thanks for sharing with us.
Mon Mar 26 23:38:35 -0700 2012
Votre article a constitué pour la plupart du temps des informations confuses que j’ai lu sur ce sujet. Le libellé clair, unique et intéressant dans votre article fait de cette plus facile à comprendre. C’est du matériel de lecture très agréable. protective clothing
Sun Apr 01 10:27:56 -0700 2012
When designing the new router, we all agreed that it should be built first as a standalone piece of functionality, with Rails sugar added on top. As a result, we used rack-mount, which was built by Josh Peek as a standalone Rack router. binary options brokers
Mon Apr 09 14:40:06 -0700 2012
provided by Optical testking training programs to help the candidates. This does not only provide Cisco Optical questions and answers but give the opportunity to students to go through the Optical pass4sure
Thu Apr 12 09:45:00 -0700 2012
Another improvement in Rails 3 bridges this gap. In Rails 3, PostsController.action(:index) returns a fully valid Rack application pointing at the index action of PostsController. So main#home is simply a shortcut for MainController.action(:home), and it otherwise is identical to providing a Sinatra application. MBA essay editing
Sun Apr 15 13:08:27 -0700 2012
This one is very nicely written and it contains many useful facts. I am happy to find your distinguished way of writing the post.
Thu Apr 19 00:09:28 -0700 2012
the most important intercontinental forum enthusiastic to take in hand the type of weather difficulty.free mothers day poems
Thu Apr 19 02:05:50 -0700 2012
cheap wedding dresses on sale under $50
dresses