Click here to Skip to main content
13,199,311 members (62,385 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How to include api in ruby on rails? for example twitter api
Posted 23-Dec-12 4:47am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

1)create an api using rails new appname
2)then create twitter app[^]
3)add Inside your gemfile add the Twitter gem:
gem 'twitter'
4)rails g model tweet tweet_content:string
5) rails g controller tweet
in controller
 require "rubygems"
require "twitter"
class TweetController < ApplicationController
 def user_page
 @tweet =
 def user_tweet
 @tweet =[:tweet])
 if then
 # Certain methods require authentication. To get your Twitter OAuth credentials,
 # register an app at
 Twitter.configure do |config|
 config.consumer_key = 'key'
 config.consumer_secret = 'skey'
 config.oauth_token = 'token'
 config.oauth_token_secret = 'stoken'
 # Initialize your Twitter client
 client =
 # Post a status update
 render action: 'user_page', :notice => 'Twitter successfully posted'
in model
class Tweet < ActiveRecord::Base
 # attr_accessible :title, :body
 attr_accessible :tweet_content
in user_page.html.erb
<%= form_for(@tweet, :url => '/user_tweet') do |tweet_form| %>
 <%= tweet_form.text_area :tweet_content %>
 <%= tweet_form.submit "Tweet" %>
<% end %>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 23 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100