Rap Genius" /> Rap Genius - 源码之家" />

生成路径上Rap Genius

问题描述:

歌曲更简单的方法有其在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 

的也许不是定义两个蛞蝓你可以只是一个有标题和艺术家作为它的一部分。所以,你的路线将是:

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' 

的路线,你可能能够得到解决的。希望这至少给你看看正确的地方的想法。