Forum: Ruby on Rails Conditional Get

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
09ca7bd1bc03a735ed913a9de3f4370b?d=identicon&s=25 Mindtonic (Guest)
on 2009-04-21 19: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.

Thanks!
09ca7bd1bc03a735ed913a9de3f4370b?d=identicon&s=25 Mindtonic (Guest)
on 2009-04-21 23:07
(Received via mailing list)
Is RESTful Rails the answer?
782b01570270ab10543bc25a258d7ea8?d=identicon&s=25 Dmitry Sokurenko (Guest)
on 2009-04-21 23: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
   end
 end
 # otherwise cached will be used
end

See:
http://ryandaigle.com/articles/2008/10/25/what-s-n...

Dmitry
09ca7bd1bc03a735ed913a9de3f4370b?d=identicon&s=25 Mindtonic (Guest)
on 2009-04-28 18: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!

Thanks!

Jay

On Apr 21, 5:13 pm, Dmitry Sokurenko <Dmitry.Sokure...@gmail.com>
782b01570270ab10543bc25a258d7ea8?d=identicon&s=25 Dmitry Sokurenko (Guest)
on 2009-04-28 19: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
designed.

Dmitry
09ca7bd1bc03a735ed913a9de3f4370b?d=identicon&s=25 Mindtonic (Guest)
on 2009-04-28 21: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:
http://books.google.com/books?id=XUaErakHsoAC&pg=P...
around page 245.

Thanks for your help.

Jay

On Apr 28, 1:00 pm, Dmitry Sokurenko <Dmitry.Sokure...@gmail.com>
This topic is locked and can not be replied to.