Task G: One Last Wafer Thin Change #1

Nicolas:

First add these two lines to routes.rb:

map.connect ':controller/:action.:format'
map.connect ':controller.:format'

This handles the .xml or .html extension for store.xml or store/index.xml

Then edit the index action in store_controller.rb:

def index
  @products = Product.find_products_for_sale
  @count = increment_count
 
  respond_to do |format|
    format.html
    format.xml { render :xml => @products.to_xml() }
  end
end

That's it!

Ahmed:

I wanted to use .rxml templates instead of @products.to_xml so I used

def index
  @products = Product.find_products_for_sale
  @cart = find_cart
 
  respond_to do |format|
    format.html
    format.xml
  end
 
end

but to my surprise, the store's layout is rendered and then the .rxml is rendered inside the body of the html which is an unwanted behavior. I didn't figure out how to solve this yet !

Ahmed:

Solution to my previous problem is

format.xml {render :template=>'store/index.rxml', :layout=>false}

Adam:

I used an .rxml template for this also. Here goes:

xml.product_list() do
  for p in @products
    xml.product do
      xml.title(p.title)
      xml.description(p.description)
      xml.price(p.price)
    end
  end
end

Anthony Ettinger says:

I opted for a more customizable .rxml template, using the first method described above, although I kept all "feeds" in the info_controller.rb

xml.price({:currency => 'USD'}, p.price)

I also added an attribute to denote which currency the price is in, possibly adding more controllers for displaying the price in different currencies: ie "/info/store.xml?curr=EUR"

Yields the following tag:

<price currency="USD">4.99</price>

It is important to note, that if you keep it in the info_controller.rb, then you should add a filter to authorize everything except the public store feed.

before_filter :authorize, :except => 'store'

This will password protect "who_bought", as I'm sure nobody wants their customer addresses leaking in an xml feed.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License