Forum: Ruby on Rails Conditional Get

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Mindtonic (Guest)
on 2009-04-21 21:08
(Received via mailing list)
Does anyone have any suggestions on how to create Conditional Get
requests in rails?  I understand the basic philosophy of the concept,
I was just wondering if there was an easy way (a helper or plugin) to
automatically generate the proper http headers when checking on
whether a file has been updated on a server.  Ideally, I would like to
generate a conditional get in an if statement, followed by the caching
algorithm if the conditional get returns the document.

Mindtonic (Guest)
on 2009-04-22 01:07
(Received via mailing list)
Is RESTful Rails the answer?
Dmitry S. (Guest)
on 2009-04-22 01:27
(Received via mailing list)
Yep, there are built in functionality to support conditional requests
(bot etags and last modified). Just use it in the controller action:

def some_action
 if stale?(:last_modified => @post.updated_at.utc, :etag => @post)
   respond_to do |format|
     # generate a full response
 # otherwise cached will be used


Mindtonic (Guest)
on 2009-04-28 20:55
(Received via mailing list)
Thanks for the conditional_get tip.

When looking at the api for stale? and fresh_when, I see that these
methods are part of action controller.  I would like to use this
methodology to determine if an rss feed should be updated or not.  The
validation process all resides in the Feed model and a related module,
so when I try to use the stale? method, I get an undefined method
error.  Would it be better to recreate the source code in my module,
or is this the first indication that what I am trying to do is going
about it the wrong way?  I would very much like to keep the process in
a module, as I am using a template pattern to define several different
caching methods for experiments on speed and agility.

Any suggestions or advice you may have on this subject would be
greatly appreciated!



On Apr 21, 5:13 pm, Dmitry S. <removed_email_address@domain.invalid>
Dmitry S. (Guest)
on 2009-04-28 21:01
(Received via mailing list)
In short — you're doing it in the wrong way. Caching should be handled
on the controller layer, unless you really need something advanced
(and I don't think you need it). So rethink the way your app is

Mindtonic (Guest)
on 2009-04-28 23:31
(Received via mailing list)
I agree, and appreciate your insight.

I want to be the RECIPIENT of either the "304 Not Modified" result, or
the document if it has been modified.  If I am reading the
documentation properly, It is describing ways to handle a request and
return the message, so I am looking for the other end of the stick.

I have been successful at generating my desired results using open-
uri.  Detailed information is available here:
around page 245.

Thanks for your help.


On Apr 28, 1:00 pm, Dmitry S. <removed_email_address@domain.invalid>
This topic is locked and can not be replied to.