我想通过URL访问用户配置文件: root/user/(username)
截至目前我已经与它合作root/user/(id)但我希望它更友好,并且可以与URL中的用户名共享.
这就是我目前的设置方式
#user_controller.rb
class UserController < ApplicationController
def profile
@user = User.find(params[:id])
end
end
#routes.rb
match 'user/:id' => 'user#profile'
#profile.html.erb
<h1><%= @user.firstname %>'s Profile</h1>
Run Code Online (Sandbox Code Playgroud)
基本上我试图做的是改变了:id的:username.我已经从设计中创建了用户模型中的用户名,所以我知道它正在运行.但是现在当我尝试在URL中获取用户名时,我得到了Couldn't find User with id=username.