2010-05-10 70 views
0

歌曲更簡單的方法有其在routes.rb定義爲像/lyrics/The-notorious-b-i-g-ft-mase-and-puff-daddy/Mo-money-mo-problems路徑:生成路徑上<a href="http://rapgenius.com" rel="nofollow noreferrer">Rap Genius</a>

map.song '/lyrics/:artist_slug/:title_slug', :controller => 'songs', :action => 'show' 

當我要生成這樣的路徑,我使用song_url(:title_slug => song.title_slug, :artist_slug => song.artist_slug)。不過,我更願意能夠輸入song_url(some_song)。有沒有一種方法,我可以做到這一點,除了定義像一個幫手:

def x_song_path(song) 
    song_path(:title_slug => song.title_slug, :artist_slug => song.artist_slug) 
    end 

回答

0

的也許不是定義兩個蛞蝓你可以只是一個有標題和藝術家作爲它的一部分。所以,你的路線將是:

map.song '/lyrics/:slug', :controller => 'songs', :action => 'show' 

然後在模型中定義返回蛞蝓一個to_param:

class Song < ActiveRecord::Base 
    def to_param 
    artist.gsub(/\W+/, '-') + '/' + title.gsub(/\W+/, '-') 
    end 
end 

我不能完全肯定是否會工作(它可能會嘗試編碼「 /」但如果你改變

map.song '/lyrics/*:slug', :controller => 'songs', :action => 'show' 

的路線,你可能能夠得到解決的。希望這至少給你看看正確的地方的想法。

相關問題