ruby on rails - Rescuing from Twitter Gem -



ruby on rails - Rescuing from Twitter Gem -

i have tweets_controller

#called when user submits twitter form def message unless current_user session[:twitter_message] = params[:twitter_message] #sets message form it's available send_tweet in tweet.rb after pass through omniauth redirect_to '/auth/twitter' #redirects authorize via omniauth/twitter , create user else @auth = authorization.find_by_user_id(current_user) tweet.update_status(@auth, params[:twitter_message]) redirect_to edit_user_path(current_user), :notice => "tweet sent." end end

i'm trying rescue when status update fails. want display flash message user, -- far can seem get:

def self.update_status(auth, msg) @token = auth.token @secret = auth.secret @message = msg @t = twitter::client.new twitter.configure |config| config.consumer_key = '[key]' config.consumer_secret = '[secret]' config.oauth_token = @token config.oauth_token_secret = @secret config.gateway = '[gateway_url]' end ret = @t.update(@message) tweet ||= tweet.create_from_response(ret, auth.id) rescue twitter::error => e logger.error "#{e.message}." end

how error message can display user through controller?

you can create , throw custom exception based on application.

in app/lib/could_not_update_status_error.rb

class couldnotupdatestatuserror < standarderror end

then in model:

rescue twitter::error => e logger.error "#{e.message}." raise couldnotupdatestatuserror.new("could not update status")

and in controller

else begin @auth = authorization.find_by_user_id(current_user) tweet.update_status(@auth, params[:twitter_message]) redirect_to edit_user_path(current_user), notice: "tweet sent." rescue coundnotupdatestatuserror => e # error stuff end

another alternative rescue homecoming false in twitter::error clause , wrap update_status phone call in if statement, exceptions more robust solution.

ruby-on-rails ruby-on-rails-3 twitter-gem

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -