Rails routing for an online shop, eg: /somecategory/someitem, causes issues with gets for /javascripts/all.js
I'm having issues with rails routing for an online shop. I want routes eg:
/cars/camaro
/bikes/nightrod
To achieve this, i've got this in routes.rb:
match '/:cat/:item', :to => 'browse#item'
It works fine, as in i can browse all good. But it causes issues with http GET's for (mostly i've noticed) '/javascripts/all.js' - these appear to be being routed to my browse#item action wrongly, which crashes because it cannot find that category or product.
Can someone suggest how i can solve this? Through better routing? I'd rather not give up my cool url's, but a last resort is, i guess, to route to /browse/category/product...
FWIW i'm hosting开发者_如何学Go in heroku.
Obviously you've now created a default path for everything, which isn't a great solution. I haven't actually tried this, but I think :constraints might be what you're looking for... i.e.
match '/:cat/:item', :to => 'browse#item', :constraints => { :cat => /(cars|bikes|trucks|vans)/ }
but any time a cat changes, you'll have to add that there...
Alternatively if your categories are stored in the DB, you can try:
match '/:cat/:item', :to => 'browse#item', :constraints => { :cat => /#{Category.all.map{|c|c.name}.join('|')}/ }
精彩评论