Forum: Ruby on Rails Possible to use dashes instead of underscores in rails?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2006-04-27 06:39
(Received via mailing list)
Hi all -

My boss is asking me if we can have dashes instead of underscores in the
URL for SEO reasons.  Apparently google prefers "foo-bar" to "foo_bar".
The former will be found when searching for just "foo", but not the
latter.

So, I'm wondering if there is a way without changing any of my code
if I can tell rails to use dashes instead of underscores?

Any ideas?
3324a49b1605f0e2df7676ff64f5e712?d=identicon&s=25 Mohammad Ruby (mohammad)
on 2006-04-27 06:49
Philip Hallstrom wrote:
> Hi all -
>
> My boss is asking me if we can have dashes instead of underscores in the
> URL for SEO reasons.  Apparently google prefers "foo-bar" to "foo_bar".
> The former will be found when searching for just "foo", but not the
> latter.
>
> So, I'm wondering if there is a way without changing any of my code
> if I can tell rails to use dashes instead of underscores?
>
> Any ideas?

Im a n00b but, maybe look into mapping it like this:
map.connect 'foo-bar', :controller => "foo_bar"
3324a49b1605f0e2df7676ff64f5e712?d=identicon&s=25 Mohammad Ruby (mohammad)
on 2006-04-27 06:49
edit
 Im a n00b but, maybe look into mapping it like this:
 map.connect '/foo-bar', :controller => "foo_bar"
3324a49b1605f0e2df7676ff64f5e712?d=identicon&s=25 Mohammad Ruby (mohammad)
on 2006-04-27 06:50
edit
Im a n00b but, maybe look into mapping it like this:
map.connect '/foo-bar/:action', :controller => "foo_bar"
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-04-27 06:58
(Received via mailing list)
How would this affect say a link_to call in a view...

say

link_to "some place", :controller => "does_this_need_underscores",
:action
=> "act"

Would this then be mapped to the dash route?
3324a49b1605f0e2df7676ff64f5e712?d=identicon&s=25 Mohammad Ruby (mohammad)
on 2006-04-27 07:15
I think so but rendering I would test
<a href="/contoller/action">Text</a>
so map should fix. As for rendering that needs to be ran by a test
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-27 08:19
Philip Hallstrom wrote:
> Hi all -
>
> My boss is asking me if we can have dashes instead of underscores in the
> URL for SEO reasons.  Apparently google prefers "foo-bar" to "foo_bar".
> The former will be found when searching for just "foo", but not the
> latter.
>
> So, I'm wondering if there is a way without changing any of my code
> if I can tell rails to use dashes instead of underscores?
>
> Any ideas?

map.connect 'foo-bar/:action/:id',
  :controller => 'foo_bar',
  :action => nil,
  :id => nil

url_for :controller => 'foo_bar'
  #=> /foo-bar

url_for :controller => 'foo_bar',
        :action     => 'narf'
  #=> /foo-bar/narf

url_for :controller => 'foo_bar',
        :action     => 'show',
        :id         => @foo.id

  #=> /foo-bar/show/123


Setting the routing params to nil tell the router they are optional.
This topic is locked and can not be replied to.