Ajmal Afif's blog

Balancing between consuming knowledge and creating values.

Instagram gem with Rails

I use Instagram gem for my favorite person’s birthday Rails project:

http://happy-birthday.daiyantrisha.com

Here’s how I done it:-

Step 1

Register here

Step 2

Inside Gemfile;

Gemfile
1
2
3
    gem 'sqlite3'
    
    gem 'instagram'

Step 3

Inside your Rails app directory; $ bundle install

Step 4

Copy your client id from http://instagram.com/developer/clients/manage/ and paste it here for your access token.

Step 5

Create instagram.rb in config/initializers

instagram.rb inside config/intializers
1
2
3
4
5
6
7
8
9
    require "instagram"

    Instagram.configure do |config|

    config.client_id = "client_id goes here"

    config.access_token = "access_token goes here"

    end

Step 6

instagram.rb inside config/intializers
1
$ rails g controller Home index

Step 7

Inside /app/controllers/home_controller.rb;

home_controller.rb
1
2
3
4
5
6
7
    class HomeController < ApplicationController

    def index
        @instagram = Instagram.user_recent_media("user_id_here", {:count => 1})
    end

    end

Get your user_id here

Step 8

Inside app/views/home/index.html.erb;

1
2
3
4
5
    <% @instagram.each do |instagram| %>

    <%= image_tag instagram.images.standard_resolution.url %>

    <% end %>

Step 9

Inside config/routes.rb, remove;

1
        get "home/index"

and replace with;

1
        root :to => "home#index"

Step 10

Remove index.html

1
     $ rm public/index.html
Resource links

Comments