11175
(-- --)
May 3, 2008, 4:35pm
1
my beginner's luck has vaporized on this one. new to rails; new to
ruby; but been around the block several times.
my model: (Category.rb)
class Category < ActiveRecord::Base
self.to_xml(:only => [:id, :name], :include => [:entries] )
has_many :entries
end
without the to_xml mod, everything works fine. With this line in place,
I get the following error:
undefined method 'to_xml' for class:0xb76bef9c
Application Trace:
/usr/lib/ruby/gems/activerecord-2.0.1/lib/active_record/base.rb:1459: in
'method_missing'
Please help!!!!!
should I switch to using rexml?
With that code above, you’re trying to call the class method #to_xml on Category. That method doesn’t exist. #to_xml is an instance method inherited from ActiveRecord::Base. If you want to customize #to_xml , do something like this:
class Category < ActiveRecord::Base
has_many :entries
def to_xml
# your changes here
end
end
Regards,
Craig
Yes, your controller would do something like this:
class CategoriesController < ApplicationController
…
def show
@category = Category.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @category.to_xml(:only => [:id, :name], :include => [:entries] ) }
end
end
…
end
Regards,
Craig
11175
(-- --)
May 3, 2008, 5:53pm
5
Craig Demyanovich wrote:
def show
@category = Category.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @category.to_xml (:only => [:id,
:name], :include => [:entries] ) }
Perfect! Thanks so much for the help!