Go back to the main page

Rails etag with non-database related data

This article is over 2 years old. Proceed with caution.

Regards ♨ – Minimul

 

# lib/categories.rb
module Categories
  BOOKS = [ { isbn: 2333343, title: "To Kill a Robin", account: 4454 }, 
  .... # omitted

# app/controllers/books_controller.rb
def data
  @categories = Categories
  fresh_when :last_modified => File.mtime(Rails.root + 'lib/categories.rb').utc, :etag => @categories
end

# app/views/books/data.js.erb
var bookData = <%= @categories::BOOKS.to_json %>

Comment on this article?