Topic: Dashes (-) in URL's

Hi all

Was wondering if someone could help me. We've recently started moving a lot of our static sites over to rails purely because it's a power framework and the sites are open for expansion. We require that "multi-worded" url's have dashes in the e.g. http://domain.com/pages/two-worded-url for SEO

The way it's currently running is that we have a pages controller and then different actions for the static pages.

class PagesController < ApplicationController
  layout "www_default"
 
  def home
    @page_title = 'Home'
  end
 
  def contact
    @page_title = 'Contact Us'
  end
 
end

This works fine for the following url http://domain.com/pages/home/

How would I go about getting the following url http://domain.com/pages/web-marketing/ working as the following code causes an error

...

   def web-marketing
     @page_title = 'Web Marketing'
   end

...


Thanx in advance guys

Re: Dashes (-) in URL's

I was looking for this exact same thing last week and stumbled upon this article
http://www.seoonrails.com/to_param-for- … oking-urls
It was an easy solution to implement and has given me the dashed urls that I was looking for. Hope this helps.

Re: Dashes (-) in URL's

WOW!!! that was an insanely fast reply. Thank you very much smile

Unfortunately it's not the solution i'm searcning for and i'm refering to a comment in that entry, as it's the exact problem i'm having. Maybe i'm missing something in that article

Brian McQuay wrote:

How would you manage the url rewriting in routes.rb? In particular, if my pretty url is /pretty-url/new and the routes are :controller/:action , this doesn't work. You can't name your controller with a -. How do you propose parsing out - for controller names in routes.rb but still having pretty urls? Thanks

Re: Dashes (-) in URL's

you could use routes for this I think.

map.web-marketing :controller => 'pages', :action => 'web_marketing'

not tested

but you could put it into the params[:id] to make it more dynamic

class PagesController < ApplicationController
  layout "www_default"
 
  def show
    @page = page_find_by_name(params[:id])
  end

 
end


and have the page particulars in the db so it can fetch the details for each page with the one action.

--

Re: Dashes (-) in URL's

Thanks BasicMind

For these flat 5 page sites db is not really the route we'd like take... But i've kept your snippet of code because it definatly looks useful

Re: Dashes (-) in URL's

hi all... just an update. I've decided to insert every dashed static page manually (as there won't be many) in routes.rb like so

map.connect 'pages/web-marketing', :controller => 'pages', :action => 'web_marketing'

Thanx to everyone who tried to find a solution