2011-09-01 57 views
1

我一直在我的最新的Rails应用程序中使用Twitter的宝石,迄今没有任何问题。我已经注册了应用程序,已经在config/initializers/twitter.rb中设置了API令牌,并且已经测试过它可以在需要gem的自定义rake测试中工作。但问题是,当我尝试从控制器发送推文时,什么都不会发生。我的初始化看起来像这样:Twitter的宝石不工作从控制器在Rails

require 'twitter' 

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

###在我的应用程序正确填写,很明显。在我的rake文件中,我需要文件顶部的gem,然后可以发送测试推文Twitter.update(tweet)但是,相同的语法不适用于我的控制器。

我在这里做错了什么?我是否需要重新初始化控制器中的宝石?

+0

经过一番修补之后,我想出了自己的想法。见下文。 – bjork24

回答

2

一些修修补补之后,这是简单的解决方案:

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

并称,到我的控制器的方法非常完美,因为当应用程序启动的Twitter客户端已经通过身份验证。这是应用程序发送推文,顺便说一下,不是用户通过应用程序发送推文,所以我不需要重新进行身份验证。

1

我也使用Twitter的宝石,我使用和授权控制器为我的誓言,直接消息控制器的Twitter的DM和在前面的Ajax。该AppConfig只是一个yml文件,其中有我的信用。

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 

我的过滤器之前 '授权' 只检查会话:

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

希望这有助于。