我有两个型号:如何让我的网址看起来像www.example.com/1/product/date/price?
Price :belongs_to Products
Product :has_many Prices
我创造了新的价格,我想使URL看起来一定的方式,所以在我Price
模式,我把:
def to_param
"#{id}/#{product.name}/#{purchase_date}/#{price}".parameterize
end
但是,这给了我一个URL,如:
http://localhost:3000/prices/8-turkey-bacon-2012-01-16-2-58
当理想我希望它是:
http://localhost:3000/8/turkey-bacon/2012-01-16/$2.58
有没有一种方法,我可以做到这一点?
的routes.rb - 价格仅
resources :prices do
get ":id/:product_name/:purchase_date/:price" => "prices#show"
get :autocomplete_product_name, :on => :collection
post :create_multiple, :on => :collection
end
尝试没有了'parameterize' – yoavmatchulsky
@yoavmatchulsky这只是抛出一个路由错误,当我打的索引页来查看所有价格。 – LearningRoR