Easier way to generate paths
Songs on Rap Genius have paths like /lyrics/The-notorious-b-i-g-ft-mase-and-puff-daddy/Mo-money-mo-problems
which are defined in routes.rb
as:
map.song '/lyrics/:artist_slug/:title_s开发者_Go百科lug', :controller => 'songs', :action => 'show'
When I want to generate such a path, I use song_url(:title_slug => song.title_slug, :artist_slug => song.artist_slug)
. However, I'd much prefer to be able to type song_url(some_song)
. Is there a way I can make this happen besides defining a helper like:
def x_song_path(song)
song_path(:title_slug => song.title_slug, :artist_slug => song.artist_slug)
end
Perhaps instead of defining two slugs you could just have one that has both the title and artist as part of it. So your route would be:
map.song '/lyrics/:slug', :controller => 'songs', :action => 'show'
Then in your model define a to_param that returns the slug:
class Song < ActiveRecord::Base
def to_param
artist.gsub(/\W+/, '-') + '/' + title.gsub(/\W+/, '-')
end
end
I'm not completely sure if that will work (it might try to encode the "/" although if you change the route to
map.song '/lyrics/*:slug', :controller => 'songs', :action => 'show'
you might be able to get around that. Hopefully that at least gives you ideas of the right place to look.
精彩评论