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.
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2008-12-08 13: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
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2008-12-08 14:38
(Received via mailing list)
On Mon, Dec 8, 2008 at 1:55 PM, Dave Smith
<rails-mailing-list@andreas-s.net> 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...
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2008-12-08 16:08
Franz Strebel wrote:
> On Mon, Dec 8, 2008 at 1:55 PM, Dave Smith
> <rails-mailing-list@andreas-s.net> 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.