Creating a seo-friendly URL from a string

Hi guys, I'm trying to create a url for a link from a string. Instead of example.com/Jessica Alba I want example.com/jessica-alba

How do I get link_to tags to link me to my friendly permalink?

I also need to make sure that the show method only displays the seo permalink in the address bar and only accepts the seo permalink.

+6
source share
3 answers

You can override the to_param method in your model.

So, if you have a model called Celebrity that has a name column, you can go:

 class Celebrity < ActiveRecord::Base def to_param self.name.downcase.gsub(' ', '-') end end 

Then:

  jessica_alba = Celebrity.find_by_name("Jessica Alba") link_to "Jessica Alba", celebrity_path(jessica_alba) 
+4
source

It is best to use the parameterize method:

 name = name.parameterize 

Parameter Inflector #

+27
source

check out the has_permalink gem that I created at http://haspermalink.org

This stone will help you with this.

+2
source

Source: https://habr.com/ru/post/891887/


All Articles