Conditional Get

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!

Is RESTful Rails the answer?

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-new-in-edge-rails-even-better-conditional-get-support

Dmitry

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 S. [email protected]

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

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.

Jay

On Apr 28, 1:00 pm, Dmitry S. [email protected]