问题描述
我正在尝试对事件控制器中的show方法进行一些更改,并注意到这些更改使差异为零。我注释掉@event = Event.find_by_slug(params [:slug])行,并且show视图仍然有效,并且不会产生错误!我什至删除了整个show方法,它仍然有效。我想了一下,我正在开发该应用程序的副本,但这绝对是正确的。
I'm trying to make some changes to the show method in my events controller and noticed the changes making zero difference. I commented out the @event = Event.find_by_slug(params[:slug]) line and the show view still works and does not produce an error! I even deleted the entire show method and it still works. I thought for a moment I was working on a copy of the app, but it's definitely the correct one.
我以前从未遇到过此问题,但是最近确实将我的Rails版本从3.2.0升级到了3.2.13。想知道某处是否有缓存设置会导致这种情况。有没有人经历过类似的事情,或者是否有关于在哪里寻找缓存配置设置的指针?
I've never had this problem before, but did recently upgrade my Rails version from 3.2.0 to 3.2.13. Wondering if there's a caching setting somewhere that's causing this. Has anyone experienced similar or got any pointers on where to look for a caching config setting perhaps?
编辑-添加代码
def show
@event = Event.find_by_slug(params[:slug])
@meta_title = "#{@event.headline} at #{@event.venue.name}, #{@event.venue.town} - #{@event.event_date.to_date.to_formatted_s(:my_format)}"
@meta_description = "#{@event.info.to_s.truncate(380, :separator => " ")}"
@facebook_image = "#{@event.event_image.url(:large)}"
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @event }
end
end
视图很大,但是我会使用类似这样的字段来调用字段:
The view is quite large, but I would call fields using something like this:
<h2><%= @event.headline %> TEST</h2>
'TEST'是我刚刚添加的东西,以查看是否可以渲染,所以我'肯定是在编辑正确版本的应用。
'TEST' is something I just added to see if that would be rendered and it is, so i'm definitely editing the correct version of the app.
编辑-发现此错误的新进展
EDIT - New development in finding this bug
之后广泛搜索欺诈行为等,我逐渐开始尝试手动查找原因。首先,通过搭建新模型并查看是否发生了相同的行为。然后查看事件控制器中从上到下的不同之处,我开始注释行/动作,然后测试行为。无论如何,注释掉我用来调用CanCan gem的 load_and_authorize_resource
,它的能力模型导致我的应用程序按其应有的方式运行,显然现在没有基于角色的代码。
After extensive searches for dupe actions etc I gradually started trying to manually find the cause. First by scaffolding a new model and seeing if the same behaviour occurred and it didn't. Then looking at what was different, top to bottom in my event controller I started to comment out lines/actions and then test behaviour. Anyway, commenting out load_and_authorize_resource
which I am using to call the CanCan gem and it's ability model the caused my app to behave as it should do, obviously now without my role based code.
谁能想到为什么CanCan可能会导致这种情况?
Can anyone think why CanCan could be causing this?
推荐答案
可以同时具有 load_resource
, authorize_resource
和 load_and_authorize_resource
。它们都按照他们说的去做,因此在您的示例中, load_resource
设置 @event
实例变量 @event = Event.find(params [:id])
。 authorize_resource
只会调用 authorize! @event
。 load_and_authorize_resource
将首先加载资源,然后像以前一样进行授权。
CanCan have both load_resource
, authorize_resource
and load_and_authorize_resource
. They all do what they say, so in your example will load_resource
set the @event
instance variable @event = Event.find(params[:id])
. authorize_resource
will just call authorize! @event
before each action. load_and_authorize_resource
will fist load the resource and then authorize as before.
这里是:
class ArticlesController < ApplicationController
load_and_authorize_resource
def show
# @article is already loaded and authorized
end
end
这篇关于缓存控制器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!