我正在使用Omniauth和Twitter运行rails 4应用程序。我试图获得接近producthunt.com的东西,在那里他们验证用户并使用他们的Twitter用户名作为他们的网址ID。
答案 0 :(得分:0)
据我所知,您希望网址如下所示:example.com/users/username 而不是 example.com/users/123
如果是这样,您所要做的就是更改在用户(或任何您称为用户模型)控制器中找到de correct用户的方式。目前它可能看起来像这样:
#Users Controller
def show
@user = User.find(params[:id])
end
# Your path to that user is probably this:
user_path(123) #basically you pass the user.id
上面的代码使用 Model.find(#)来查找用户。 .find()通过其id#查看用户。相反,您希望通过用户名而不是ID来查找它。要执行此操作,请使用 Model.find_by 您可以查看查询here的所有方法。 此外,每当您查找查找用户显示页面的路径时,不必将ID#发送到URL,您现在必须发送用户名字符串。
您的新设置应如下所示:
#Users Controller
def show
@user = User.find_by :username params[:id]
#this assumes you have it in your DB as username. Some twitter apps save it as screen_name.
end
# Your path to that user is probably this:
user_path('username') #basically you pass the username instead. current_user.username? I dont know what you call in in your app.
希望有所帮助。如果您有疑问,请告诉我。