Forum: Ruby on Rails where to initialize a variable in an AJAX based application

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
84a473cc8e6842426532f3e4b9abe1f2?d=identicon&s=25 Carl Harroch (Guest)
on 2006-05-23 11:57
Hello All,

I am getting confused with best practices. I posted a topic earlier but
realized how confusing it was. Thus I will do my best to be as clear as
possible.

I am a bit confused on best practice for instanciate a variable that
will be used accross several methods within the same controller. I need
this because of AJAX, here is a description of what I intend to do:

(all this in done using AJAX in a single page)

1. User enter postcode -> calls method 1

2. Method 1 queries a web service (initialization of the wsdl =
overhead) -> returns a list of addresses for that postcode to the user

3. User select address -> calls method 2 (same controller as method 1)

4. Method 2 queries the web service a again to get the details ->
redisplay the address to the user.

My problem is that I instanciate a variable in method 1 that will deal
with the overhead of scanning the WSDL. When I call method 2, it
reinstanciate that variable thus loosing a couple of seconds. I have the
following:
---
class PostcodeController < ActionController::Base

  def display_address
   @p =
PostcodeAnywhereService.new("http://services.postcodeanywhere.co.uk/uk/lookup.a...)
    res = @p.by_postcode(params[:postcode])
    @array_Interim_result = res.byPostcodeResult.results.interimResult
  end

  def grab_address
    @p =
PostcodeAnywhereService.new("http://services.postcodeanywhere.co.uk/uk/lookup.a...)
    res = @p.fetch_address(params[:pca_id])
    @address_result = res.fetchAddressResult.results.address
  end
end
---

I would like to not initialize the @p in the grab_address method. The
value is not kept between display_address and grab_address (if I take
the first line of the grab_address,   I end up with a nil value for p)


Is it possible to create a variable within the same controller,
initialize it, and use it along for a same user. I can not get this to
work. Any information is greatly appreciated.

Thanks,
Carl
This topic is locked and can not be replied to.