What you need is FriendlyId which is covered in this RailsCasts episode.
Rails routing for pretty urls and get variables
-
28-06-2022 - |
سؤال
Rails noob here
I have a url "/products?sports=2", which shows the products fine. But I want the url to be "/volleyball"
I currently have, which I know is completely worng:
match 'volleyball' => 'products?sports=2'
Any suggestions? Thanks.
المحلول
نصائح أخرى
Check out the friendly_id gem.
You may want the following route:
get ':sport_id', :to => 'products#show'
class ProductsController < ApplicationController
def show
render text: "sport is #{params['sport_id']}"
end
end
لا تنتمي إلى StackOverflow