Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:04:50+00:00 2026-06-14T04:04:50+00:00

How do I expire a fragment in the cache with a file extension (.json)

  • 0

How do I expire a fragment in the cache with a file extension (.json) from a another controller?

I am using Rails 3.2.8.

I have two controllers for the same models: /admin/books_controller and /api/books_controller. The admin controller is used to create/update/delete books and requires elevated access. The api/books_controller is used to search/list view details about a book by users. I’ve attempted to cache the api/books_controller

class Api::BooksController < ApiController
  caches_action :show

  def show    
    @book = Book.find(params[:id])
  end
end

This works great. The first request is slow and the second is fast

Rendered api/books/show.json.rabl (119.7ms)
Write fragment views/localhost:3000/api/books/4.json (1.7ms)
Completed 200 OK in 568ms (Views: 124.2ms | ActiveRecord: 9.5ms)

Read fragment views/localhost:3000/api/books/4.json (0.2ms)
Completed 200 OK in 16ms (ActiveRecord: 2.6ms)

Now, I need to expire the the fragments when the Book is updated or destroyed. I have this:

class Admin::BooksController < AdminController

  def update
    @book.attributes = params[:book]
    if (@book.save)
      expire_action(:controller => 'api/books', :action => 'show', :id => @book.id, :format => 'json')
    end
  end

end

However, this doesn’t resolve to the same fragment (the request is an HTML request now, to a different controller):

Started PUT "/admin/books/4" for 127.0.0.1 at 2012-11-08 12:27:11 -0600
Processing by Admin::BooksController#update as HTML
...
Expire fragment views/localhost:3000/api/books/4 (0.1ms)

My API controller with a JSON request reads and writes this fragment:

views/localhost:3000/api/books/4.json

My Admin controller tries to expire this fragment:

views/localhost:3000/api/books/4

How do I expire views/localhost:3000/api/books/4.json from the other controller with a different request format?

This question: rails 3 caching: expire action for named route indicates I could use a regex to expire multiple fragments, but that requires the keys in the cache to be enumerable (http://api.rubyonrails.org/classes/ActionController/Caching/Fragments.html#method-i-expire_fragment)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:04:51+00:00Added an answer on June 14, 2026 at 4:04 am

    It appears that the problem was with routing setting default parameters. In the API controller, params[:format] was ‘json’ and in the Admin controller it was nil.

    When I added this to my Admin controller:

    params[:format] = 'json'
    expire_action(:controller => 'api/books', :action => 'show', :id => @book.id, :format => 'json')
    

    Then, the fragments in both controllers matched.

    This let me back to my routes:

    In my routes I had this:

    namespace :api, :defaults => {:format => 'json'} do
      resources :books, :only => [:show]
    end
    

    This caused params[:format] to be set in my api namespace. I didn’t have anything in my admin namespace and apparently actionpack/ActionCachePath saw these two differently. Removing the defaults in the routes fixed the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Devise as authenticating solution in Rails and I have a cached fragment
I'm using fragment caches pretty extensively and have ran into a few gotchas where
How to expire a directory in Rails? I have a blog where /posts/ lists
If you have a session timeout in rails using the :expire_after in session_store.rb AppRails3::Application.config.session_store
Our app uses Rails.cache in the controller to cache some items outside the scope
I am trying to interact with the cache store in a Rails app using
I have a Rails application with database that is updated from external C++ program.
I'm using Rails 3 with Memcached to cache some models. When the model changes,
I want to add expire TIMESTAMP in DB table. I have one field timestamp
If I set a far future expire header for this file (take note of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.