CanCan,设置嵌套资源?
我有以下模型:
Group (id)
Poll (id, group_id)
PollVote (id, poll_id)
我不想进行深度嵌套,这意味着我不想 /group/:id/poll/:id/poll_vote/:id
我想设置它,所以我的路线:
/group/:id
/poll/:id
/poll/:id/poll_vote/:poll_vote_id
我有 poll工作,但我不知道如何让 PollVote 工作...到目前为止,我有:
class PollVotesController < ApplicationController
# Authorization w Devise & CanCan
before_filter :authenticate_user! # Devise, signed in users only
load_and_authorize_resource :poll # CanCan
load_and_authorize_resource :poll_vote, :through => :poll
# We need to pass along the wall
def current_ability
@current_ability ||= Ability.new(current_user, @poll.group_id)
end
然后在能力.rb 中
can [:manage], Poll do |poll|
This returns TRUE is the user is a group member of the poll
end
我在 PollVotes 中使用什么,让 PollVotes 使用 Poll 检查 CanCan?
谢谢
I have the following models:
Group (id)
Poll (id, group_id)
PollVote (id, poll_id)
I don't want to do deep nesting, meaning I don't want /group/:id/poll/:id/poll_vote/:id
I want to set it up so my routes:
/group/:id
/poll/:id
/poll/:id/poll_vote/:poll_vote_id
I have poll working, but I can't figure out how to get PollVote working... So far I have:
class PollVotesController < ApplicationController
# Authorization w Devise & CanCan
before_filter :authenticate_user! # Devise, signed in users only
load_and_authorize_resource :poll # CanCan
load_and_authorize_resource :poll_vote, :through => :poll
# We need to pass along the wall
def current_ability
@current_ability ||= Ability.new(current_user, @poll.group_id)
end
Then in ability.rb
can [:manage], Poll do |poll|
This returns TRUE is the user is a group member of the poll
end
What do I use in PollVotes, to have PollVotes check CanCan using Poll?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未向用户显示<->组关联,因此如果
User has_and_belongs_to_many :groups
则:当然,您可能希望将其锁定为仅与用户关联的投票。
编辑 - 以下是限制创建和编辑投票的访问权限的示例:
You haven't shown your User <-> Group association, so if
User has_and_belongs_to_many :groups
then:Of course, you probably want to lock that down to only votes associated with the user.
Edit - here are examples for restricting access to creating and editing poll votes: