Routing issue

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 https://github.com/norman/friendly_id
to create and manage slugs):
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

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

Any ideas?

Best Regards
Linus

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 :slight_smile:

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

Best Regards
Linus

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

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 :slight_smile:

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?

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? :slight_smile:

Best Regards
Linus

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

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.

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.

Thank you.

I'll keep it like this for now :slight_smile:

Anyone else has any thoughts in this matter?

Best Regards
Linus