我遇到了一个小问题...我设置了一个服务于德国网站的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
”。
有什么想法?!?
问候,
塞巴斯蒂安
真棒迈克!这正是我所期待的!不知道为什么我没有在“地图”中找到它 - 文档... – Sebastian 2009-01-29 16:50:17