# Routing issue

**URL:** <https://www.ruby-forum.com/t/routing-issue/204734>\
**Category:** Rails\
**Created:** [March 19, 2011, 11:53pm UTC](https://www.ruby-forum.com/t/routing-issue/204734 "2011-03-19T23:53:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 19, 2011, 11:53pm UTC](https://www.ruby-forum.com/t/routing-issue/204734/1 "2011-03-19T23:53:27Z")

</div>

Hi!

I have two models, artists and songs.

I have this in my route file:  
resources :artists, :path =\> “ackord” do  
resources :songs  
end

Which gives me urls like this (I’m using

> **[GitHub - norman/friendly\_id: FriendlyId is the “Swiss Army bulldozer” of...](https://github.com/norman/friendly_id)**
>
> FriendlyId is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids fo...

to create and manage slugs):  
[example.com/ackord/artistname/songs/songname-id](http://example.com/ackord/artistname/songs/songname-id)

Now I want to remove the /songs part to get a url like this:  
[example.com/ackord/artistname/songname-id](http://example.com/ackord/artistname/songname-id)

Is it possible? I have tried to do it with match without success.

Any ideas?

Best Regards  
Linus

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [March 21, 2011, 11:42am UTC](https://www.ruby-forum.com/t/routing-issue/204734/2 "2011-03-21T11:42:08Z")

</div>

Any ideas anyone?

I tried this:

match ‘ackord/:artist\_id/:id’ =\> ‘songs#show’, :as =\> :song

resources :artists, :path =\> “ackord” do  
resources :songs  
end

Which makes it work without the /songs/ part in the url at least for  
the show action. It still works with the /song/ part though, which I  
don’t want. Duplicate urls are not good 🙂

Also, I have to create a match for each action with this approach. Is  
there a better way to do this?

Best Regards  
Linus

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [March 21, 2011, 3:54pm UTC](https://www.ruby-forum.com/t/routing-issue/204734/3 "2011-03-21T15:54:45Z")

</div>

Excerpts from Linus P.'s message of Mon Mar 21 03:40:43 -0700  
2011:

> Which makes it work without the /songs/ part in the url at least for  
> the show action. It still works with the /song/ part though, which I  
> don’t want. Duplicate urls are not good 🙂
> 
> Also, I have to create a match for each action with this approach. Is  
> there a better way to do this?

# config/routes.rb

resources :artists, :path =\> ‘ackord’ do  
resources :songs, :path =\> ‘’  
end

# rake routes

artist\_song GET /ackord/:artist\_id/:id(.:format)  
{:action=\>“show”, :controller=\>“songs”}

## Does this work for you Linus?

med vnlig hlsning  
David J. Hamilton

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [March 21, 2011, 4:38pm UTC](https://www.ruby-forum.com/t/routing-issue/204734/4 "2011-03-21T16:38:46Z")

</div>

Hi and thank you for your reply!

It kinda works… There is some issues though.

When I go to …/ackord/artist-name I want to load the artists show  
action and inside this I loop out all the songs that’s related to the  
artist.

When I go to this url now it load the songs index action instead. Its  
because I have these two in my routes

artist\_songs GET /ackord/:artist\_id(.:format)  
{:action=\>“index”, :controller=\>“songs”}  
and  
artist GET /ackord/:id(.:format)  
{:action=\>“show”, :controller=\>“artists”}

Perhaps this is actually better and instead of showing the songs  
inside artist#show I filter the songs and show them in songs#index  
instead.

Any suggestions on either approach? Would it even matter? 🙂

Best Regards  
Linus

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [March 22, 2011, 1:58pm UTC](https://www.ruby-forum.com/t/routing-issue/204734/5 "2011-03-22T13:58:11Z")

</div>

Thank you.

I’ll keep it like this for now 🙂

Anyone else has any thoughts in this matter?

Best Regards  
Linus

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [March 21, 2011, 5:04pm UTC](https://www.ruby-forum.com/t/routing-issue/204734/6 "2011-03-21T17:04:32Z")

</div>

Excerpts from Linus P.'s message of Mon Mar 21 08:37:33 -0700  
2011:

> artist\_songs GET /ackord/:artist\_id(.:format)  
> {:action=\>“index”, :controller=\>“songs”}  
> and  
> artist GET /ackord/:id(.:format)  
> {:action=\>“show”, :controller=\>“artists”}
> 
> Perhaps this is actually better and instead of showing the songs  
> inside artist#show I filter the songs and show them in songs#index  
> instead.

Personally I think it’s fine for this logic to live in the songs  
controller,  
especially given that you’ve effectively namespaced the songs (by  
artist) by  
making it a nested resource.

That said, there is a bit of an ugliness in the route collision,  
especially  
since rails presumably doesn’t guarantee the how the collision is  
resolved.

OTOH there’s a lot of ugliness in having to specify each route manually.  
Unless  
someone else on the list has a better suggestion than the one I offered,  
you may  
have to simply pick your poison.

–  
med vnlig hlsning  
David J. Hamilton
