No suggestions? Did I make the question too complicated?
On 2/13/06, Bamat Est <bamatest@gmail.com> wrote:> I''ve got a block of code that''s turning out.. well - just
plain ugly.
> So i know I must not be doing it the Ruby way.
>
> The main problem I''m having is in dealing with handling errors -
and
> just not finding much Rails documentation that gives a deeper
> understanding of how to handle errors in Rails (outside of several
> docs on the catch-all''s)
>
> the main method i''m having problem with is a method in my
controller
> that recieves shipping information about a customer from my order
> system and is supposed to return a shipping label back to the user.
>
> The problem is that i want to validate that the form is sending me all
> the required fieds using ActiveRecords validation rules, then I need
> to submit the address in the order via XML to an external address
> verification service, use that data to make any changes to the order
> and then submit the order to the shipper via XML and recieve back
> their response - interpret it and then display the shipping label to
> the end user if everything went through succesfully.
>
> So i could fail on ActiveRecord Validation - or either of those two
> XML request can send back an error code. In all three cases I want to
> send the user back to the form they came from and display either the
> active record errors or the XML error messages.
>
> You can see my two "hackish" approaches to catching the errors
with
> the XML errors with an if else in the verfication and a rescue at the
> bottom to catch times when the success values i expect are not
> present. But trying to add in the ActiveRecord validation at the top
> (go.save) causes the error to fall straight down to the rescue clause
> even when i try to intercept it with if else clauses, etc. Using
> redirect_to :back was another hack i don''t like. It sends the user
> back to whichever page they came from (5 potential input forms) - but
> it loses all the form data that they submitted into the order.
>
> I pretty much hate the way this is working today, but was under a
> timeline crunch to get it done by today so I swallowed my pride and
> hacked it up. But I''ve got an extra day now and would really love
it
> if someone could point me in the right direction to fixing these
> mistakes. Everything i''m trying only seems to make the code uglier
and
> uglier.
>
> def label
> require ''fileutils''
> require ''xmlsimple''
>
>
> @submitted = params[:FedExOrder]
>
> go = FedExOrder.new(params[:FedExOrder])
> go.save
>
>
> @fedex = Shipping::FedEx.new(
> :name => @submitted[''name''],
> :company => @submitted[''company''],
> :phone => @submitted[''phone''],
> :email => @submitted[''email''],
> :address => @submitted[''address''],
> :address2 => @submitted[''address2''],
> :city => @submitted[''city''],
> :state => @submitted[''state''],
> :zip => @submitted[''zip''])
>
> verification = XmlSimple::xml_in(@fedex.verify_address)
>
> if verification[''Match'']
> if
verification[''Match''][0][''Score''].to_s ==
''100''
> @fedex.address =
verification[''Match''][0][''Line1''].to_s
> @fedex.address2 =
verification[''Match''][0][''Line2''].to_s
> @fedex.city =
verification[''Match''][0][''City''].to_s
> @fedex.state >
verification[''Match''][0][''StateOrProvinceCode''].to_s
> else
> flash[:notice] =
verification[''SoftError''][0][''Message'']
> redirect_to :back
> return
> end
> else
> flash[:notice] =
verification[''Error''][0][''Message'']
> redirect_to :back
> return
> end
>
> mylabel = @fedex.label
> @label = "#{mylabel.tracking_number}.png"
> FileUtils.copy_file mylabel.image.path,
"/localpath//#{@label}"
>
> rescue
> flash[:notice] = mylabel.message
> redirect_to :back
> return
> end
>