Hey, Guys!
I have next action in controller products:
def who_bought
@product = Product.find(params[:id] )
respond_to do |format|
format.html
format.xml { render :xml => @product.to_xml( :include => :order )
}
format.json {render :json => @product.to_json(:include => :order )
}
end
end
routes.rb
…
resources :products do
get :who_bought member: :on
end
…
I want check, how work in xml format. How me call in command line my
format xml? (format.xml { render :xml => @product.to_xml( :include =>
:order ) })
On Sun, Mar 31, 2013 at 5:17 AM, Dmitrij B. [email protected]
wrote:
I want check, how work in xml format. How me call in command line my
format xml? (format.xml { render :xml => @product.to_xml( :include =>
:order ) })
I guess you want to see what’s the output in rails console
Just open it and then do
Product.find(SOME_ID).to_xml
then you’ll get the info
For example i can see in command line: curl --silent
http://localhost:3000/products/3/who_bought.atom.builder.
And how me call in command line format.xml?
pass accept header in curl: -H “Accept: application/xml”
2013/3/31 Dmitrij B. [email protected]
“Ruby on Rails: Talk” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
–
Pagarbiai,
Gintautas
i add curl --silent http://localhost:3000/products/3/who_bought -H
“Accept: application/xml”
show error:
NoMethodError
in ProductsController#who_bought
undefined method `order' for #<Product:0xb6196084>
Rails.root: /home/dima/RubyOnRails/Projects/depot
its my product.rb:
class Product < ActiveRecord::Base
has_many :line_items, dependent: :destroy
has_many :orders, through: :line_items
before_destroy :ensure_not_referenced_by_any_line_item
attr_accessible :title, :description, :image_url, :price
validates :title, :description, :image_url, :price, :presence => true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{ .(gif|jpg|png)$}i,
message: 'gif, jpg png. ’
#}
#validates :image_url, :uniqueness => false
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, " have products line")
return false
end
end
end
On Sun, Mar 31, 2013 at 1:55 PM, Dmitrij B. [email protected]
wrote:
i add curl --silent http://localhost:3000/products/3/who_bought -H
“Accept: application/xml”
According to that url you’re going to action “who_bought” on
“products_controller”
and the problem is inside that action
undefined method `order' for #<Product:0xb6196084>
It seems you are doing Product.order, but your model says that you have
a
has_many relation with orders
so it should be Product.orders (maybe I’m wrong I’m not sure what’s
inside
your “who_bought” action)
Sorry, I haven’t seen the action that you post in the beginning
def who_bought
@product = Product.find(params[:id] )
respond_to do |format|
format.html
format.xml { render :xml => @product.to_xml( :include => :order )
}
format.json {render :json => @product.to_json(:include => :order )
}
end
end
It should be @product.to_xml(:include => :orders)
Note the plural
yea) I just do not notice. Thanks for your help!