Click here to Skip to main content
16,020,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to include api in ruby on rails? for example twitter api
Posted

1 solution

1)create an api using rails new appname
2)then create twitter app https://dev.twitter.com/apps[^]
3)add Inside your gemfile add the Twitter gem:
gem 'twitter'
4)rails g model tweet tweet_content:string
5) rails g controller tweet
XML
in controller
 require "rubygems"
require "twitter"

class TweetController < ApplicationController
 def user_page
 @tweet = Tweet.new
 end

 def user_tweet
 @tweet = Tweet.new(params[:tweet])

 if @tweet.save then
 # Certain methods require authentication. To get your Twitter OAuth credentials,
 # register an app at http://dev.twitter.com/apps
 Twitter.configure do |config|
 config.consumer_key = 'key'
 config.consumer_secret = 'skey'
 config.oauth_token = 'token'
 config.oauth_token_secret = 'stoken'

 end

 # Initialize your Twitter client
 client = Twitter::Client.new

 # Post a status update
 client.update(@tweet.tweet_content)
 end
 render action: 'user_page', :notice => 'Twitter successfully posted'

 end

end
in model
class Tweet < ActiveRecord::Base
 # attr_accessible :title, :body
 attr_accessible :tweet_content
end
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 %>
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900