更改从ID到用户名的设计路线 [英] Change devise route from ID to Username

查看:111
本文介绍了更改从ID到用户名的设计路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已尝试使用该帖子来更改路线以指向用户名。我知道这很简单,但由于某种原因,我现在无法计算它。我已经在设计文档页面上尝试了所有内容。

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:

http://example.com/username

而不是

http://example.com/users/1

推荐答案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆