bjork24
bjork24

Reputation: 3183

Twitter gem not working from controller in Rails

I've been using the Twitter gem in my latest Rails app, and so far have had no problems. I've registered the app, have set the API tokens in config/initializers/twitter.rb, and have tested that it works in a custom rake test that requires the gem. The problem, however, is that when I try to send a tweet form a controller, nothing happens. My initializer looks like so:

require 'twitter'

Twitter.configure do |config|
  config.consumer_key = '###'
  config.consumer_secret = '###'
  config.oauth_token = '###'
  config.oauth_token_secret = '###'
end

The ### are filled in correctly in my app, obviously. In my rake file, I require the gem at the top of the file, and am then able to send a test tweet with Twitter.update(tweet) however, the same syntax does not work from my controllers.

What am I doing wrong here? Do I need to re-initialize the gem from the controller?

Upvotes: 1

Views: 1235

Answers (2)

bjork24
bjork24

Reputation: 3183

After some tinkering, this is the simple solution:

@twitter = Twitter::Client.new
@twitter.update(tweet)

Adding that to my controller method worked perfectly, since the Twitter client had already been authenticated when the app started. This is the app sending out tweets, by the way, not users sending tweets through the app, so I didn't need to re-authenticate.

Upvotes: 2

Chris Barretto
Chris Barretto

Reputation: 9529

I am also using the Twitter gem and I use and authorizations controller for my oath, direct messages controller for Twitter DMs, and ajax on the front. The AppConfig is just a yml file that has my creds in it.

authorizations_controller.rb

class AuthorizationsController < ApplicationController
  def new
    set_oauth
    render :update do |page|
      page.redirect_to @oauth.request_token.authorize_url
    end
  end

  def show
    @oauth ||= Twitter::OAuth.new(AppConfig['consumer']['token'], AppConfig['consumer']['secret'])
    @oauth.authorize_from_request(session['rtoken'], session['rsecret'], params[:oauth_verifier])

    session['rtoken'] = nil
    session['rsecret'] = nil
    session['atoken'] = @oauth.access_token.token
    session['asecret'] = @oauth.access_token.secret
    redirect_path = session['admin'] ? admin_tweets_path : root_path
    redirect_to redirect_path
  end
end

direct_messages_controller.rb

class DirectMessagesController < ApplicationController
  before_filter :authorize

  def create
    @client.update("@#{AppConfig['user']} #{params[:tweet][:text]}")

    render :update do |page|
      page.replace_html 'tweet_update', "Your tweet has been sent to #{AppConfig['user']} and should be updated momentarily."
    end
  end
end

view.html.haml

#tweet_update
  - form_remote_tag :url => direct_messages_url, :method => :post, :loading => "$('tweet_update').hide();$('loading').show()", :complete => "$('tweet_update').show();$('loading').hide()" do
    %div{:class => "subheader float_left"}Tweet to Whoever
    - if session_set?
      %input{:type => "image", :src=>"/images/sendButton.jpg", :class =>"float_right", :style=>"margin-bottom: 4px"}
    - else
      %div{:class => "float_right", :id => "twitter_login_button"}= link_to_remote image_tag('twitter-darker.png'), :url => new_authorization_url, :method => :get
    .float_clear
    #tweetbox_bg
      - textarea_options = {:id => "tweetbox", :style => "overflow: auto", :rows => "", :cols => ""}
      - textarea_value = nil
      - unless session_set?
        - textarea_options.merge!(:disabled => "disabled")
        - textarea_value = "Please login to tweet Whoever!"

      = text_area_tag 'tweet[text]', textarea_value, textarea_options

My before filter 'authorize' just checks session:

def authorize
  session_set? ? set_client : redirect_to(new_authorization_url)
end

Hope this helps.

Upvotes: 1

Related Questions