Hello,
I have two ActiveRecord classes. One is Route the other is Waypoints.
I have constructed an XML representation so I can use AJAX to create a
Route with all waypoints in a single create call to RouteController.
When I call Route.from_xml(xmlString) it is not returning an object -
it’s leaving the object (@route) nil, but not erroring. Any ideas?
Here’s some code:
RoutesController:
def create
@route = nil;
if params[:requestXML]
# unserialise from XML using ROXML
@route = Route.from_xml(params[:requestXML]);
else
@route = Route.new(params[:route])
end
respond_to do |format|
#The following line unsurprisingly fails, as @route is nil
if @route.save
flash[:notice] = 'Route was successfully created.'
format.html { redirect_to(@route) }
format.xml { render :xml => @route, :status
=> :created, :location => @route }
format.js
else
format.html { render :action => “new” }
format.xml { render :xml => @route.errors, :status
=> :unprocessable_entity }
end
end
end
Route.rb:
class Route < ActiveRecord::Base
include ROXML
has_many :waypoints
xml_reader :title
xml_reader :totalDist
xml_reader :totalMis
xml_reader :totalHg
xml_reader :lonlatx
xml_reader :lonlaty
xml_reader :grcenter
xml_accessor :waypoints, [Waypoint], :in => “waypoints”
end
Waypoint.rb:
class Waypoint < ActiveRecord::Base
include ROXML
belongs_to :route
xml_reader :isLeg
xml_reader :lonlatx
xml_reader :lonlaty
xml_reader :gridReference
xml_reader :ascent
xml_reader :descent
xml_reader :distance
xml_reader :bearing
xml_reader :timemins
end
NB I’ve not specified types at the moment for the above elements. I’m
just trying to verify that the XML is being parsed by ROXML’s from_xml
method.
Error Message in logs:
Processing RoutesController#create (for 127.0.0.1 at 2009-02-03
16:36:12) [POST]
Session ID:
BAh7DToMY3NyZl9pZCIlZWY5NWRjODFjMTI5ZDg3ZDlhZGRjODM0ODA3NmZl
YTE6DW5hdnN0YWNrWwYiBi86DnJldHVybl90byIaL2FkYW1mb3dsZXIvZGFz
aGJvYXJkOhBsYXN0X2FjdGl2ZVU6IEFjdGl2ZVN1cHBvcnQ6OlRpbWVXaXRo
Wm9uZVsISXU6CVRpbWUNb0MbgAAAwMEGOh9AbWFyc2hhbF93aXRoX3V0Y19j
b2VyY2lvblQiCFVUQ0AMOgl1c2VyaRM6C2ZvcnVtc3sAOgt0b3BpY3NADiIK
Zmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsG
Ogtub3RpY2UiJFJvdXRlIHdhcyBzdWNjZXNzZnVsbHkgY3JlYXRlZC4GOgpA
dXNlZHsGOxBU–48a6d44f6baa5129fd1160815273a9c30ca746a8
Parameters: {“requestXML”=>“11185.321521477119</
totalDist>640235.75000000000003</
totalMins>357865271635</
lonlaty>SH 71635 57865</
grcenter>false357290</
lonlatx>271650SH 71650 57290</
gridReference>812200</
distance>0false</
isLeg>357260274600</
lonlaty>SH 74600 57260275</
ascent>482950.152538429157</
distance>91false</
isLeg>359160273330</
lonlaty>SH 73330 5916073</
ascent>1702285.3664913969487</
distance>326false</
isLeg>359170270050</
lonlaty>SH 70050 59170182</
ascent>1723280.015243867016</
distance>270false</
isLeg>357470269740</
lonlaty>SH 69740 5747029</
ascent>1071728.0335644888382</
distance>190false</
isLeg>356840270440</
lonlaty>SH 70440 56840640</
ascent>717941.7536832951597</
distance>132”,
“authenticity_token”=>“a538a4ed8056e17ded2904abfb00628ff6594a07”,
“action”=>“create”, “controller”=>“routes”}
[globalite] loading locale: en-US from config
[globalite] Locale set to en-US
[globalite] loading locale: en-US from config
[globalite] Locale set to en-US
NoMethodError (You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.delete):
/Library/Ruby/Gems/1.8/gems/activerecord-2.1.2/lib/active_record/
transactions.rb:124:in rollback_active_record_state!' /Library/Ruby/Gems/1.8/gems/activerecord-2.1.2/lib/active_record/ transactions.rb:106:in
save’
/app/controllers/routes_controller.rb:52:in create' /Library/Ruby/Gems/1.8/gems/actionpack-2.1.2/lib/action_controller/ mime_responds.rb:106:in
call’
/Library/Ruby/Gems/1.8/gems/actionpack-2.1.2/lib/action_controller/
mime_responds.rb:106:in respond_to' /app/controllers/routes_controller.rb:51:in
create’
…
Thanks,
Adam.