Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:46:16+00:00 2026-05-25T23:46:16+00:00

I have a create method that calls a method in a model that pings

  • 0

I have a create method that calls a method in a model that pings some third-party APIs.

What I need to do is if the API sends back a certain message, then I’d display an error.

Below is my current controller and model setup, so how would I get the error back in to the controller (and ultimately the view)?

Here is the method in my controller:

def create
  @number = Number.where(:tracking => params[:number][:tracking], :user_id => current_user.id).first

  if @number.blank?
    @number = Number.new
    @number.tracking = params[:number][:tracking]
    @number.user_id = current_user.id
    @number.notes = params[:number][:notes]
    @number.track
  end

  respond_with(@number) do |format|  
    format.html { redirect_to root_path }  
  end
end

Here are the methods in my model:

def track
  create_events
end


def create_events(&block)

  tracker = fedex.track(:tracking_number => number)

  if tracker.valid?    
    self.assign_tracker(tracker)

    tracker.events.each do |e|
      self.create_event(e) unless (block_given? && !block.call(e))
    end

    save
  else
    # NEED TO THROW THE ERROR HERE
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:46:17+00:00Added an answer on May 25, 2026 at 11:46 pm

    How about if rather than throwing errors, you just use validation? Something like the following (Just to get your started. This would need work.):

    # if you don't cache the tracker in an attribute already, do this so
    # you can add errors as if it were a column.
    attr_accessor :tracker
    
    def create_events(&block)
    
      tracker = fedex.track(:tracking_number => number)
    
      if tracker.valid?    
        # ...
      else
        # add the error with i18n
        errors.add(:tracker, :error_type_if_you_know_it)
    
        # or add it from a returned message
        errors.add(:tracker, nil, :message => fedex.get_error())
      end
    end
    

    Then in your controller:

    @number.track
    
    respond_with(@number) do |format|
      if @number.errors.any?
        format.html { redirect_to root_path }
      else
        format.html { render :some_template_with_errors }
      end
    end
    

    Alternatively you could do this as part of validation (so calling valid? would work as expected and not destroy your custom “track” errors)

    # do your tracking on creation, if number was given
    validate :on => :create do
      if number.present?
        tracker = fedex.track(:tracking_number => number)
        unless tracker.valid?
          errors.add :tracker, nil, :message => tracker.get_error()
        end
      end
    end
    
    # then do your actual creation of tracking events sometime after validation
    before_save :handle_tracker_assignment
    
    def handle_tracker_assignment
      self.assign_tracker(tracker)
      # note the block method you're using would need to be reworked
      # ...
    end
    

    Note in the latter case you’d have to change your logic a bit, and simply pass the tracking number and attempt to save a new record, which would trigger the tracking attempt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unit test that creates a mock calls my method to be
Hi I want to create a WCF service that have login method, which is
Just random pick some methods that I have found: sforce.connection.describeSObject sforce.SearchResult sforce.sObject sforce.LeadConvert() sforce.connection.create()
I have some calls that must execute sequentially. Consider an IService that has a
I have a particular mock that is being handled by a third party. I
I have a model that I overrode the save method for, so that the
I have created a method that should ideally take a single Account object and
I have a method that creates a local textfield and pops up the keyboard
I have created a new method in one of the project's controllers that it
I have created a gem that adds a new method to the class ActiveRecord::Base.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.