11155
(-- --)
March 31, 2013, 10:17am
1
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 ) })
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
11155
(-- --)
March 31, 2013, 6:09pm
3
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"
11155
(-- --)
March 31, 2013, 6:55pm
5
i add curl --silent http://localhost:3000/products/3/who_bought -H
"Accept: application/xml"
show error: <h1>
NoMethodError
in ProductsController#who_bought
</h1>
<pre>undefined method `order' ; for #<Product:0xb6196084></pre>
<p><code>Rails.root: /home/dima/RubyOnRails/Projects/depot</code></p>
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
According to that url you're going to action "who_bought" on
"products_controller"
and the problem is inside that action
<pre>undefined method `order' ; for #<Product:0xb6196084></pre>
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
11155
(-- --)
March 31, 2013, 9:17pm
8
yea) I just do not notice. Thanks for your help!