RoR 中的 SEO 友好 URL [英] SEO friendly URLs in RoR
问题描述
假设我有一个用于编辑音乐专辑的相对基本的 CRUD 应用程序.在我的数据库中:
Let's say I have a relatively basic CRUD application for editing music albums. In the database I have:
id | album_name | artist | navigation
1 Lets Talk Lagwagon lets-talk
但是,我希望 albums/lets-talk
可以访问该页面,而不是 albums/1
返回我的 Show
页面路线.
However, instead of albums/1
returning my Show
page, I want the page to be accessible by the albums/lets-talk
route.
所以在我的控制器中我有:
So in my controller I have:
def show
@album = Album.find_by_navigation(params[:id])
end
在我的索引视图中,我有:
And in my index view I have:
<%= link_to 'Show', :controller => "albums", :action => "show", :id => album.navigation %>
这成功地执行了它的功能,但是,Ruby API 说我的 link_to 方法是旧的和过时的,没有列出替代方法,所以我怀疑我这样做是错误的.
This successfully performs its function, however, the Ruby API says my link_to method is old and archaic without listing an alternative, so I suspect I'm going about this the wrong way.
推荐答案
查看 friendly_id gem.这正是您所需要的.
Check out friendly_id gem. It is exactly what you need.
这篇关于RoR 中的 SEO 友好 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!