Forum: Ruby on Rails names and nested routes?!?

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.
Dave S. (Guest)
on 2008-12-08 14:55
Hi Guys,

I have the following route to show the articles for a selected service
in my application.

http://localhost:3000/services/4/articles

I want to be able to have a route to the service name rather than the
route such as the following.

http://localhost:3000/services/clothes/articles

I thought something like the following in the routes.rb file would be
the way forward?

map.connect 'clothes/', :controller => 'articles', :action => "index",
:service_id => '4'

Many thanks in advance

Dave
Franz S. (Guest)
on 2008-12-08 15:38
(Received via mailing list)
On Mon, Dec 8, 2008 at 1:55 PM, Dave S.
<removed_email_address@domain.invalid> wrote:
>
> http://localhost:3000/services/clothes/articles

Assuming that the attribute of the Service is called name,
you should define this in your Service model:

def to_param
  name
end

for more info, look at this post:

http://www.jroller.com/obie/entry/seo_optimization...
Dave S. (Guest)
on 2008-12-08 17:08
Franz S. wrote:
> On Mon, Dec 8, 2008 at 1:55 PM, Dave S.
> <removed_email_address@domain.invalid> wrote:
>>
>> http://localhost:3000/services/clothes/articles
>
> Assuming that the attribute of the Service is called name,
> you should define this in your Service model:
>
> def to_param
>   name
> end
>
> for more info, look at this post:
>
> http://www.jroller.com/obie/entry/seo_optimization...

I used the following code in the end, as I have a field title in the
table.

  def to_param
      id.to_s+'-'+title.downcase.gsub(' ', '-')
  end

However it puts the address with the ID at the beginning;

http://localhost:3000/services/1-horse-&-groom/articles

Is there a way I can remove this from the address to tidy it up a bit?
This topic is locked and can not be replied to.