Scoping require class loading, etc
I have a file booking.rb in my model directory. It contains the class definition
class Booking < ActiveRecord::Base
def self.setup_connection
wsdlurl = 'http://10.14.47.201:7001xxx/webservice/RetrieveBookingsByCCLV1_01.wsdl'
factory = SOAP::WSDLDriverFactory.new(wsdlurl)
@@driver = factory.create_rpc_driver
@@driver.return_response_as_xml = true
end
end
I attempt to invoke this method from my application.rb see code below.
module PNR2
class Application < Rails::Application
...
...
Booking.setup_connection
end
end
This fails with the following when I run the 开发者_开发技巧app...
C:/Users/sg0209028/RubymineProjects/PNR2/config/application.rb:49:in `<class:Application>': uninitialized constant PNR2::Application::Booking (NameError)
from C:/Users/sg0209028/RubymineProjects/PNR2/config/application.rb:18:in `<module:PNR2>'
from C:/Users/sg0209028/RubymineProjects/PNR2/config/application.rb:17:in `<top (required)>
The reason for the reference to line 49 is that I removed all the comments in this application.rb file to avoid taking space in this note. Line 49 in the original was the Booking.setup_connection line.
I am clearly not understanding how name scoping is working in Rails 3. Maybe I also don't understand when I should be invoking a class method to set up a constant in a Model object. It feels like that should be an application initialization task, but maybe that request should be somewhere else.
Ib case anyone is wondering, I have had this code and invocation of the appropriate web services working in a straingth Ruby (non rails) environment.
Below is the code for that
require 'soap/wsdlDriver'
require 'rexml/document'
require 'soap/rpc/driver'
WSDL_URL = "http://10.14.47.202:7001/xxx/webservice/RetrieveBookingsByCCLV1_01.wsdl"
factory = SOAP::WSDLDriverFactory.new(WSDL_URL)
driver = factory.create_rpc_driver
driver.return_response_as_xml = true
params = {"ccl" => "Booking[BookingName[BookingNameItem[TicketNumber > \"123456789\"]]]", "xmlFormat" => "DefaultBooking"}
response = driver.RetrieveBookingsByCCL(params)
doc = REXML::Document.new response
puts "Number of PNRs = " + REXML::XPath.first(doc, "//count").text
doc.elements.each ("//originCity") {|element| puts "Element = " + element.text}
Can anyone please give this newbie some pointers? Oh and yes I do realize that invoking some SOAP based services instead of back ending with a database is going to have some issues. That I am prepared for once I get the connection to work!
Thanks in advance
Chris
I just realized you were trying to use the Booking class in your application.rb file. I think you are going to have issues because at this point your application is not fully configured, but if you can get around those, to actually use the model, you'll have to require the file at the top of your application.rb file:
require 'rails/all'
require File.join(File.dirname(__FILE__), '../app/models/booking')
Booking
is in the root-namespace, and you're calling the function (on your line 49) within the PNR2::Application
namespace. You should change it to:
module PNR2
class Application < Rails::Application
...
...
::Booking.setup_connection
end
end
精彩评论