我创建了生成脚手架的模型对象,但现在我想而不是链接到
/:controller/:id (/objectname/1) 到 /:controller/:title (/objectname/new_blog_post)。我该如何做到这一点,以便链接能够正确链接到标题而不是 ID?
我想从:
/:controller/:id
to
/:controller/:name
用于参数
class User < ActiveRecord::Base
def to_param
name.blank? ? id : name
end
end
或者查看一个插件,acts_as_sluggable 和Friendly_id 是我所知道的。
编辑:哦,是的,如上所述,确保您使用的任何内容都是唯一的。
EDIT:
它会像这样工作:
class UsersController < ActionController::Base
def index
@users = User.all
end
end
In view:
<% @users.each do |user| %>
<%= link_to user.name, user_path(@user.id) %>
<% end %>
如果该用户名为 John,那么在您单击该链接后它将呈现 /users/John
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)