更改从ID到用户名的设计路线 [英] Change devise route from ID to Username
问题描述
我已尝试使用该帖子来更改路线以指向用户名。我知道这很简单,但由于某种原因,我现在无法计算它。我已经在设计文档页面上尝试了所有内容。
I have tried using the post to change the routes to point at the username. I know this is simple, but for some reason I can't compute it at the moment. I have tried everything on the devise documentation page as well.
我只想让路由布局使用用户名的id,没有用户前缀。喜欢:
I just want to have the routes layout to use the username instead of id and not have the users prefix. Like:
而不是
推荐答案
class User < ActiveRecord::Base
def to_param
username
end
end
$ b $你的控制器中的b
in your controller make
@user = User.find_by_username(params[:id])
而不是
@user = User.find(params[:id])
这将使您的路线像 http://example.com/users/username
要做出你想要的,你可以这样做:
to make what you want, you can do route like:
resources :users, :path => '' do
# nested resources...
end
所以,user_path (@user)将使URL http://example.com/username
但它不是良好的做法,因为它不是REST。我建议您离开网址,如 http://example.com/users/username
so, user_path(@user) will make url http://example.com/username but It's not a good practice, cause it's not a REST. I advise you to leave urls like http://example.com/users/username
这篇关于更改从ID到用户名的设计路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!