I currently have URLs which look like this:
things?category_id=6&country_id=17
and I would like to have URLs which look like this:
/printer_cartridges/united_kingdom
Is there a way in Rails 3, without hard coding all of the categories and countries in the router to have the URLs as I would like above, perhaps using find_by_name
or the such like? What is the best way to approach this?
URLs that are simple, easy to read, and include keywords that describe the content on a web page are SEO-friendly. For example, if you're searching for information about pancakes, a URL like https://en.wikipedia.org/wiki/pancake will help you decide to click on that link.
SEO friendly URLs are URLs that are designed to meet the needs of users and searchers. Specifically, URLs optimized for SEO tend to be short and keyword-rich.
But to ensure the URL to be SEO friendly we need to change the “id” to contain a name. There can be multiple records with the same name existing in the database. That creates an issue while using a name of a record as “id” or key. Here, I will be using the name and id combination to get the uniqueness for the view.
match '/:category_slug/:country_slug', :to => 'things#index'
Then you'll need to update your action to look up everything using params[:category_slug] and params[:country_slug] instead of the ids. Look at the slugged gem to generate slugs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With