2009-01-29 51 views
2

我遇到了一个小问题...我设置了一个服务于德国网站的rails应用程序。为了利用Rails的内部多元化功能,我将所有模型保留为英文(例如模型“JobDescription”)。 现在,如果我打电话“http://mysite.com/job_descriptions/”,我得到我所有的job_descriptions ....迄今为止,这么好。因为我不想在我的网址英文术语“job_descriptions”,我把下列我的routes.rb在rails中重命名路由(地图,link_to,to_param)

map.german_term '/german_term', :controller => 'job_descriptions', :action => 'index' 
map.german_term '/german_term/:id', :controller => 'job_descriptions', :action => 'show' 

如果我称之为“http://mysite.com/german_term/”或“http://mysite.com/german_term/283”我知道我所有的job_descriptions,这是精细。

但是,为了使URL更加适合搜索引擎优化,我想在URL中更换用户友好的插件。因此,我把我的job_description.rb如下:

def to_param 
"#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}" 
end 

其中,每当我在任何link_to方法使用“job_description_path”,使我的网址出类似“http://mysite/job_descriptions/13-my-job-description-title”。

但是,这是我卡住的地方,我想得到“http://mysite/german_term/13-my-job-description-title”。我已经尝试在link_to代码中将“job_description_path”与“german_term_path”进行交换,但仅生成“http://mysite/german_term/13”。显然,to_param未被调用。 一个解决办法,我发现是建立与链接:

<%= link_to job_description.name, german_term_path(job_description.to_param) %> 

但是,这是相当繁琐改变这一切在我的代码link_to电话。我想要的是当它出现在URL中时,用“german_term”代替“job_description”。

有什么想法?!?

问候,

塞巴斯蒂安

回答

7

我想你将需要使用宁静的路线助手来得到你想要的。

在这种情况下,它不会花费太多的重新分解(假设您已将JobDescriptions映射为资源)。留下你to_param的是,改变你的JobDescriptions路线,像下面这样:

map.resources :job_descriptions, :as => 'german_term' 

希望这有助于!

+0

真棒迈克!这正是我所期待的!不知道为什么我没有在“地图”中找到它 - 文档... – Sebastian 2009-01-29 16:50:17

0

的Rails仅使用,当你使用的是宁静的路线/链接帮手

def to_params 
end 

URL生成器。我知道的唯一方法就是和你的做法类似,除非你愿意放弃你的英语语言链接,并用德语完成。在这种情况下,只需删除命名的路由行并将to_params更改为使用数据库中正确的名称字段即可。此时,REST路由应该正常运行。