将 Devise 与单一资源结合使用
我是 Rails 新手,正在尝试使用 Devise 开发我的第一个 Rails 3 应用程序。我遇到的情况是,我网站上的几乎所有内容都是特定于某个用户的。我正在创建一种不面向公众的私人画廊,以便每个图像都属于一个用户,并且一个用户拥有许多图像。这是我的问题...我想重新设计我的路线,以便显示用户图像不需要 URL 中的用户 ID。我将在用户/显示路径中显示用户的图像。
当前路线(来自 rake:routes):
/users/show(.:format) {:action=>"show", :controller=>"users"}
是否可以让设计仅使用“资源”而不是“资源”?所以它会是/user/show/?我说得有道理吗?我不确定要问的术语,但我希望用户的所有功能都暗示我知道用户是谁,然后我可以在控制器中进行检查。
谢谢!
I'm a newb with Rails and am trying to get out my first Rails 3 app with Devise. I have a situation where pretty much everything on my site is specific to a user. I'm creating a kind of private gallery that isn't public facing, so that every image belongs_to a user and a user has_many images. Here's my issue... I want to rework my routes so that showing a users images doesn't require a user id in the URL. I will be showing a user's images in the user/show route.
current route (from rake:routes):
/users/show(.:format) {:action=>"show", :controller=>"users"}
Is it possible to have devise use only "resource" instead of "resources"? so it would be /users/show/? Am I making sense? I am not sure the terminology to ask, but I want all of a user's functionality to imply that I know who the user is, then I can check that in the controllers.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的控制器中,对于显示操作:
这就是您需要做的。如果您期望一个 id,那么您可以执行类似
@user = User.find(params[:id]
) 的操作,但由于您知道自己想要什么用户(在本例中,current_user
,由 Devise 公开),您不需要做任何特殊的事情。In your controller, for the show action:
That's all you need to do. If you were EXPECTING an id, then you do something like
@user = User.find(params[:id]
, but since you know what user you want (in this case, thecurrent_user
, which is exposed by Devise), you don't need to do anything special.