Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Caching: Using sweepers for actions which require parameters
    text
    copied!<p>I'm trying to use sweepers to handle my page refreshes. For refreshing index actions, etc everything works fine...but I can't seem to sweepers to interpret page parameters. If anyone can tell me what's wrong with the code below, I'd be very appreciative:</p> <p>Controller:</p> <pre><code>class PostsController &lt; ApplicationController load_and_authorize_resource cache_sweeper :post_sweeper, :only =&gt; [:create, :update, :destroy] caches_page :index caches_page :show caches_action :edit, :new # This refreshes cache correctly def index @posts = Post.all end </code></pre> <p># This creates cache, but does not refresh it (ever). If I place the expire_page command directly into the action (instead of the sweeper), it works fine</p> <pre><code>def update @post = Post.find(params[:id]) respond_to do |format| if @post.update_attributes(params[:post]) flash[:notice] = t(:post_updated) format.html { redirect_to(@post) } format.xml { head :ok } else format.html { render :action =&gt; "edit" } format.xml { render :xml =&gt; @post.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>The sweeper:</p> <pre><code>class PostSweeper &lt; ActionController::Caching::Sweeper observe Post def after_create(record) expire_cache_for_index(record) end def after_update(record) expire_cache_for_index(record) expire_cache_for_post(record) expire_cache_for_edit(record) end def after_destroy(record) expire_cache_for_index(record) expire_cache_for_post(record) expire_cache_for_edit(record) end private def expire_cache_for_index(record) expire_page :controller =&gt; 'posts', :action =&gt; 'index' end def expire_cache_for_post(record) expire_page :controller =&gt; 'posts', :action =&gt; 'show', :id =&gt; record.id end def expire_case_for_edit(record) expire_action :controller =&gt; 'posts', :action =&gt; 'edit', :id =&gt; record.id end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload