Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.75 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I get my URL's to look like www.example.com/1/product/date/price?
  2. Price :belongs_to Products
  3. Product :has_many Prices
  4.        
  5. def to_param
  6.     "#{id}/#{product.name}/#{purchase_date}/#{price}".parameterize
  7. end
  8.        
  9. http://localhost:3000/prices/8-turkey-bacon-2012-01-16-2-58
  10.        
  11. http://localhost:3000/8/turkey-bacon/2012-01-16/$2.58
  12.        
  13. resources :prices do
  14.   get ":id/:product_name/:purchase_date/:price" => "prices#show"
  15.   get :autocomplete_product_name, :on => :collection
  16.   post :create_multiple, :on => :collection
  17. end
  18.        
  19. require 'uri'
  20.  
  21. class Price < ActiveRecord::Base
  22.   def to_param
  23.     URI.encode("/#{id}/#{product.name}/#{purchase_date}/#{price}")
  24.   end
  25. end
  26.        
  27. # in routes.rb
  28.  
  29. get ":id/:product_name/:purchase_date/:price" => "controller#action"