SEO-friendly urls: Replacing the '-' with '%20'

Hi, I’m currently getting to grips with routing in rails, and it’s going
pretty well. However, I have a small issue with inputting elements into
the url which are more than one word, for example

is how I’d like to format the url that is typed into the url, however,
the problem I am having is how to to convert the query part
(rice-and-cheese) into this (“rice and cheese”)… I’ve tried using gsub
within routes, with no success.

How do you do this in routing? Or does it have to be don by the action
that is handling the query parameter?

Assuming I’m reading you correctly – which appears to be that you
want a recipe called ‘Rice and Cheese’ to become a url of ‘/recipes/
rice-and-cheese’ – then you want something like the PermalinkFu
plugin ( )
by Rick Olsen.

With that you can add (to the Recipe model):

has_permalink :name


def to_param; permalink; end

Then add a permalink column with a migration and replace the
Recipe.find(params[:id]) with Recipe.find_by_permalink(params[:id]) in
your RecipeController.

PermalinkFu takes care of converting all the spaces to dashes, as well
as removing other non-word characters and also does a UTF-8 to ascii
translation, so your creme brulée recipe would have a permalink of /

Alternately, assuming that you meant query literally – or if
permalink_fu is too much, then yes. The way to handle it would be in
the Controller or Model rather than in the routes. Probably the
easiest way is to call split(“-”) on the params[:id] (or whatever your
route supplies) and then do an appropriate find from there.


I have done this for SEO reasons on my application
for products and categories

in the routes.rb file i have created

named routes for categories

map.connect ‘:controller/:action/:id/:category’, :controller =>
“shop”, :action => “list”

named routes for products

map.connect ‘:controller/:action/:id/:item’, :controller =>
“shop”, :action => “view”

and have used an application_helper.rb method using gsub to change the
format of the :item or :category parameter in the url and called this
method in my views.

I’m sure that this is not the best way to do it but it does mean
search bots see a different page for every product and category in the
shop and the title of the product or the category is the name of that
page instead of realising that it is one dynamically generated page .

Of corse you can still access the pages without the :category or :item
parameter on the end of the url

I hope this helps and will be checking back to see if any one has any
better ideas.

On Aug 31, 11:57 am, Paul J. [email protected]