首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用current_user和decent_exposure进行Rails 3范围访问

使用current_user和decent_exposure进行Rails 3范围访问
EN

Stack Overflow用户
提问于 2011-05-05 16:09:37
回答 2查看 837关注 0票数 2

我成功地将decent_exposure gem设置为这个railscast

我想对编辑/更新/创建操作使用带current_user的作用域访问,以避免通过比较对象的所有者和current_user来检查权限。

控制器:

代码语言:javascript
复制
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中使用类似这样的东西,例如:

代码语言:javascript
复制
@comment = current_user.comments.find(params[:id])

提前感谢!

EN

回答 2

Stack Overflow用户

发布于 2011-07-25 11:21:40

我认为你可以这样做:

expose(:user_comments) { current_user.comments }

暴露(:user_comment)

user_comment的作用域应该是current_user.comments,并且您将在视图中使用user_comment。

票数 1
EN

Stack Overflow用户

发布于 2021-11-09 06:47:37

这应该是可行的:expose :comment, scope: ->{ current_user.comments }

https://github.com/hashrocket/decent_exposure#scope

请试一试。

Adam T,如果我猜对了,decent_exposure将在这种情况下寻找UserAnswer模型,并以未初始化的常量UserAnswer落入,因为没有这样的模型:expose(:user_comments) { current_user.comments }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5894538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档