问题
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.
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?
EDIT - Code added
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' 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
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.
Can anyone think why CanCan could be causing this?
回答1:
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.
Here is another example from the CanCan documentation on github:
class ArticlesController < ApplicationController
load_and_authorize_resource
def show
# @article is already loaded and authorized
end
end
来源:https://stackoverflow.com/questions/16749073/cached-controller-method