Go back to the main page

Rails etag with non-database related data

 

# 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 %>

  • Pushed on 05/15/2013 by Christian