试图为我的实践应用程序制定政策。在我的posts控制器中,我遇到了一个“无方法错误”。
如果我们把注意力集中在post控制器和我的更新方法上,这里是代码。
def update
authorize @post
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
#redirect_to @post
else
render :edit
format.html { render action: 'edit' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end结束
你可以看到没有什么特别的。只是更新页面的HTML和JSON呈现。
authorize代码指向专家中查找访问策略的助手。
定义在我的Admin.rb模型中。
def editor?
self.role == 'editor'
endauthorize代码查找对应于方法名称的策略。它在策略类中查找,并开始应用这里找到的业务规则。问题就从这里开始。
我到了def update? @admin.editor? end
上面写着undefined method 'editor?' for #<Class:0x007ffb7fa4f6a0>
代码位于Git:https://github.com/wmuengineer/portfolio/tree/policy上的策略分支上。
发布于 2014-04-18 03:19:55
学到的教训。正确地阅读文档。我需要用
def pundit_user
current_admin
end而且起作用了。
https://stackoverflow.com/questions/23125523
复制相似问题