Drawing routes using user name in Rails 3 w/ devise

ⅰ亾dé卋堺 提交于 2019-11-29 05:10:14

You can use scope for this:

scope ":username", :as => "user" do
  resources :things
end

Combine this with to_param on the user model:

def to_param
  username
end

And you'll have routes such as /username/things. Be careful though, the username shouldn't contain any dots, forward slashes or standard URI characters. You may want to chuck a parameterize on the end of username to make sure.

You can also use resource :user, path: ':id' do ... end

Also don't forget to define to_param in your user modal & use User.find_by_username(params[:id]) in your controller.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!