使用 Devise,如何使用户只能编辑/查看属于他们的项目
我已经被这个问题困扰了一段时间了,所以我想我应该向专家请教。
我试图让用户只能编辑/查看他们使用 Devise 创建的项目。
我已经设置了用户并且运行良好。项目是由与其关联的用户创建的,我可以通过 Rails 控制台验证这一点。
def create
@item = Item.new(params[:item])
@item.user = current_user
end
我现在想做的是让用户登录后只能看到他们创建的项目,而看不到其他项目。
在我的 Items 控制器中尝试替换:
def index
@items = Items.all
end
with
def index
@items = current_user.Items.find(params[:id])
end
但这似乎对我不起作用,我得到
undefined method `Items' for #<User:0x007fdf3ea847e0>
任何人都可以提供有关下一步尝试什么的建议吗?
非常感谢。
I've been struggling with this for a while now, so I thought I'd ask the experts.
I am trying to make it so that Users can only edit/view Items that they have created using Devise.
I have Users set up and working well. Items are being created with a user associated with them and I can verify this via rails console.
def create
@item = Item.new(params[:item])
@item.user = current_user
end
What I am trying to do now is to make it so that once logged in, users can only see the items that they have created, and no others.
In my Items controller have tried replacing:
def index
@items = Items.all
end
with
def index
@items = current_user.Items.find(params[:id])
end
but this doens't seem to work for me and I get
undefined method `Items' for #<User:0x007fdf3ea847e0>
Can anyone offer any advice as to what to try next?
Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许我是老派,但我不会使用 current_user 来查找记录,仅用于验证权限。我将直接使用主键关系(它们不会改变):
@items = Item.find(:all, :conditions => { :user_id => current_user[:id] }
或者
至于设置权限,devise 实际上不允许你这样做,但是有一个名为 Cancan 的优秀补充,你绝对应该研究它,使用 Cancan,你将有一个能力.rb 类来定义你的权限。您正在寻找的内容将变为:
阅读 Cancan 文档将澄清上面的代码。
Maybe I`m old school but I would not use current_user to find records, only to verify permissions. I would use the primary key relationships directly (they don't change):
@items = Item.find(:all, :conditions => { :user_id => current_user[:id] }
or
As for setting permissions, devise actually doesn`t let you do that BUT there is the excellent supplement called Cancan, you should definitely look into it. With Cancan, you will have an ability.rb class that will define your permissions. What you are looking for then becomes:
reading the Cancan docs would clarify the code above.
您想要做的事情非常接近......
current_user
是User
类的“实例”。您想要做的是使用用户实例中的关联,这是一种应用于每个用户的特殊方法——“
items
”。 (如果Item
类也有一个belongs_to :user
,它也会有一个名为user
的方法)您需要
current_user。 items.find(params[:id])
另外,当您创建它时,您还可以使用
current_user.items.create(params[:item])
What you're trying to do is really close…
current_user
is an "instance" of theUser
class.What you want to do is use the association from the user instance, which is a special method applied to every user—"
items
". (If theItem
class also has abelongs_to :user
it'll have a method calleduser
as well)You want
current_user.items.find(params[:id])
Also, when you create it, you could also use
current_user.items.create(params[:item])
如果我理解你的问题,我想你可能想查看一个授权库 - 比如 CanCan 来做到这一点。
https://github.com/ryanb/cancan
它可以非常灵活地处理像这样的权限类型的事情。许多人将这个库与 Devise 结合使用。
If I'm understanding your question, I think you might want to check out an authorization library - like CanCan to do this.
https://github.com/ryanb/cancan
It works pretty slick to handle permission type things like this. Many people use this library in conjunction with Devise.