Rails 3-使用 current_user 和 decent_exposure 进行范围访问
我成功地将 decent_exposure gem 设置为 railscast。
我想使用 current_user 的范围访问来进行编辑/更新/创建操作,以避免通过将对象所有者与 current_user 进行比较来检查权限。
控制器:
class CommentsController < ApplicationController
expose(:article)
expose(:comments) { article.comments }
expose(:comment)
def index
end
def new
end
def create
if comment.save
redirect_to comment.article, :notice => "Successfully created comment!"
else
render :new
end
end
end
我怎样才能在我的编辑操作中使用decent_exposure这样的东西,例如:
@comment = current_user.comments.find(params[:id])
提前致谢!
I successfully set-up the decent_exposure gem as this railscast.
I would like to use scope access with current_user for the edit / update / create action in order to avoid checking the permission by comparing the owner of object with current_user.
Controller :
class CommentsController < ApplicationController
expose(:article)
expose(:comments) { article.comments }
expose(:comment)
def index
end
def new
end
def create
if comment.save
redirect_to comment.article, :notice => "Successfully created comment!"
else
render :new
end
end
end
How can I have with decent_exposure something like this in my edit action for example :
@comment = current_user.comments.find(params[:id])
Thanks in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你可以这样做:
次暴露(:user_comments){current_user.comments}
暴露(:user_comment)
user_comment的范围应限于current_user.comments,并且你将使用user_comment将在你的视图中使用。
i think you could do:
expose(:user_comments) { current_user.comments }
expose(:user_comment)
the user_comment should be scoped to current_user.comments and you would use user_comment would be used in your view.
这应该有效:
公开:评论,范围:->{ current_user.comments }
https:// /github.com/hashrocket/decent_exposure#scope
请尝试。
Adam T,如果我猜对了,在这种情况下,decent_exposure 将寻找 UserAnswer 模型,并以未初始化的常量 UserAnswer 落下,因为没有这样的模型:
expose(:user_comments) { current_user.comments }
This should work:
expose :comment, scope: ->{ current_user.comments }
https://github.com/hashrocket/decent_exposure#scope
Please try.
Adam T, if i get right, decent_exposure will seek for UserAnswer model in this case and fall with uninitialized constant UserAnswer since there is no such model:
expose(:user_comments) { current_user.comments }