Arun Agrawal’s Blog

Ruby on Rails Developer

Rails4 App on Heroku

Updated : Heroku has updated to use ruby2.0. by default.

If you are deploying a new rails4 app on heroku that might fail. And you will get following error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
----> Ruby/Rails app detected
-----> Installing dependencies using Bundler version 1.3.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
       Fetching gem metadata from https://rubygems.org/..........
       Fetching gem metadata from https://rubygems.org/..
       Installing rake (10.0.4)
       Installing i18n (0.6.4)
       Installing minitest (4.7.4)
       Installing multi_json (1.7.7)
       Installing atomic (1.1.9)
       Installing thread_safe (0.1.0)
       Installing tzinfo (0.3.37)
       Installing activesupport (4.0.0.rc1)
       Gem::InstallError: activesupport requires Ruby version >= 1.9.3.
       An error occurred while installing activesupport (4.0.0.rc1), and Bundler cannot
       continue.
       Make sure that `gem install activesupport -v '4.0.0.rc1'` succeeds before
       bundling.
 !
 !     Failed to install gems via Bundler.
 !

 !     Push rejected, failed to compile Ruby/Rails app

In short, it says

1
Gem::InstallError: activesupport requires Ruby version >= 1.9.3.

Problem is that heroku uses ruby 1.9.2 by default

To check this run this command from your app

1
heroku run ruby -v

The output will be

1
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]

Solution is simple, we need to attach ruby-version with app to tell heroku to use ruby 1.9.3 or higher because rails4 works with ruby-1.9.3 or higher.

Now add this following in your app Gemfile

1
ruby '1.9.3'

This will force heroku to use your desired ruby. Here we are using 1.9.3

This solution will work for your rails4 versions(4.0.0.beta1, 4.0.0.rc1, 4.0.0.rc2) apps.

Switched to Heroku and Octopress

I started posting blogs using self hosted Wordpress solution. After few years I realize that I should use Ruby/Rack framework. And drop this old way of doing blog post.

I tried moving away from Wordpress few times and one day got success. Finally I converted my all blog posts into markdown format.

Now it’s very easy to choose a Ruby/Rack based framework as posts are already in markdown.

After that I bought a theme from bootstrap and applied that, but I was still using my hosted solution with middleman generated site.

I survived with this for a long time. Problem was for every blog post I have to deploy site using git pull every time by logging into server.

At last I found octopress and a Heroku combination.

And here is my blog! Using Octopress and Heroku and I love it. Simple and easy!

OAuth With OmniAuth and Twitter

Hi Folks, If you want to have OAuth in your Rails Application with twitter. OmniAuth is the best gem to use. OmniAuth provides list of  Strategies to use many OAuth for your application. Here is the List of Strategies. Showing here a Twitter Strategy for OmniAuth. Twitter uses the OAuth 1.0a flow, you can read about it here: https://dev.twitter.com/docs/auth/oauth For using Twitter OAuth you have to register a Application on Twitter (https://dev.twitter.com/apps/new) Once you done with the registration obtain the Consumer Key and Consumer Secret from the Twitter Application. Be sure to put the callback URL in the application. Callback URL is the URL where user will land after successful authentication. Showing an image here how to register an Application with Twitter.   Here showing some of the steps : Generate a new Rails Application:
rails new TwitterAuth
Update your gemfile add omniauth-twitter gem into that
gem "omniauth-twitter"
Create a config/initializers/omniauth.rb file. Paste your key instead of XXXX, and secret instead of YYYY
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, 'XXXX', 'YYYY'
end
All Done! Just start the server
bundle exec rails server
And hit the URL
http://localhost:3000/auth/twitter
And you should be landing on the Twitter Authorize page! After success your app will redirect to your given callback URL with information and token! At OmniAuth.org you can try out different -2 Strategies.   Useful links :

Respond to Custom Formats in Rails

We usually respond some of the known formats in Rails Application like HTML, XML, JavaScript, RSS and some custom. Have you tried to use your own custom format for your Rails Application? Yes you can use your custom format in Rails Application. Here showing a simple Rails Application with responding custom formats. Get a new app
rails new music_library 
Get a scaffold into App
rails generate scaffold mp3 title:string url:string description:text 
Ok so you are ready to serve some music on your app with some formats! Now you have to register MIME types in the Rails Application. For that open up Rails.root/config/initializers/mime_types.rb
Mime::Type.register 'audio/mpeg' , :mp3
Now you can serve .mp3 and content For that your respond block should look like
def show
  @mp3 = Mp3.find(params[:id])
  respond_to do |format|
    format.mp3 { redirect_to @mp3.url }
  end
end
Now if you call this action with .mp3
http://localhost:3000/mp3s/1.mp3
You will redirect_to @mp3 url. Happy adding custom formats!!