I am using caches_page to cache a controller action which returns javascript caches_page :show_county def show_county @county = County.find(params[:id]) respond_to do |format| format.js end However when I hit the page, the server caches it as a html: Cached page: /home/show_county/22.html (1.1ms) This is obviously causing problems on subsequent hits. I am using rails 2.2.2 Any ideas on why the page is cached as .html and how do I rectify this? Diarmuid
On Mon, May 11, 2009 at 2:36 AM, Diarmuid <diarmuid.m.collins-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > I am using caches_page to cache a controller action which returns > javascript > > caches_page :show_county > > def show_county > @county = County.find(params[:id]) > > respond_to do |format| > format.js > end > > > However when I hit the page, the server caches it as a html: > > Cached page: /home/show_county/22.html (1.1ms) > > This is obviously causing problems on subsequent hits. > I am using rails 2.2.2 > > Any ideas on why the page is cached as .html and how do I rectify > this? > > Diarmuid >Diamuid, this is the standard behavior of caches_page. If you''re looking to cache only HTML, I would recommend the following: caches_page :show_county, :if => Proc.new { |c| c.request.format.html? } Good luck, -Conrad --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Thanks for the response. However my problem is that I *want* it to cache the response as a *js* file (ie 22.js) and then on subsequent hits the server will return this file. I tried : caches_page :show_county, :if => Proc.new {|c| c.request.format.js? } but that didn''t help matters. Even in a Railscast I watched (http://railscasts.com/episodes/89-page- caching) I can see the correct behaviour but for some reason I cannot replicate that behaviour On May 11, 1:25 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, May 11, 2009 at 2:36 AM, Diarmuid <diarmuid.m.coll...@googlemail.com > > > > > wrote: > > > I am using caches_page to cache a controller action which returns > > javascript > > > caches_page :show_county > > > def show_county > > @county = County.find(params[:id]) > > > respond_to do |format| > > format.js > > end > > > However when I hit the page, the server caches it as a html: > > > Cached page: /home/show_county/22.html (1.1ms) > > > This is obviously causing problems on subsequent hits. > > I am using rails 2.2.2 > > > Any ideas on why the page is cached as .html and how do I rectify > > this? > > > Diarmuid > > Diamuid, this is the standard behavior of caches_page. If you''re > looking to cache only HTML, I would recommend the following: > > caches_page :show_county, :if => Proc.new { |c| c.request.format.html? } > > Good luck, > > -Conrad
OK I think I have solved this issue. Adding the solution to help others. I also have one question that I don''t undestand below, maybe someone could enlighten me The problem is that by default the caching mechanism will use the same extension of the request to store the cached file. If no extension exists (/home/show_county/22) then the default is .html Next was the figure out how to get the request to have the js extension. This is done by setting an argument to url_for :format => :js This doesn''t seem to be well documented (http://apidock.com/rails/ ActionController/Base/url_for only in the comments!) but it did work. *However as a consequence of this I had to rename some views from xxx.html.erb to xxx.erb, I don''t know why? Anyone?* Once I did this, the request was sent as a .js which was then cached as a 22.js and on subsequent hits was returned as a javascript file. Done Diarmuid On May 11, 1:45 pm, Diarmuid <diarmuid.m.coll...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Conrad > > Thanks for the response. > > However my problem is that I *want* it to cache the response as a *js* > file (ie 22.js) and then on subsequent hits the server will return > this file. > > I tried : > > caches_page :show_county, :if => Proc.new {|c| > c.request.format.js? > } > > but that didn''t help matters. > > Even in a Railscast I watched (http://railscasts.com/episodes/89-page- > caching) I can see the correct behaviour but for some reason I cannot > replicate that behaviour > > On May 11, 1:25 pm, Conrad Taylor <conra...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Mon, May 11, 2009 at 2:36 AM, Diarmuid <diarmuid.m.coll...@googlemail.com > > > > wrote: > > > > I am using caches_page to cache a controller action which returns > > > javascript > > > > caches_page :show_county > > > > def show_county > > > @county = County.find(params[:id]) > > > > respond_to do |format| > > > format.js > > > end > > > > However when I hit the page, the server caches it as a html: > > > > Cached page: /home/show_county/22.html (1.1ms) > > > > This is obviously causing problems on subsequent hits. > > > I am using rails 2.2.2 > > > > Any ideas on why the page is cached as .html and how do I rectify > > > this? > > > > Diarmuid > > > Diamuid, this is the standard behavior of caches_page. If you''re > > looking to cache only HTML, I would recommend the following: > > > caches_page :show_county, :if => Proc.new { |c| c.request.format.html? } > > > Good luck, > > > -Conrad