I am new to programming and am trying to create a clear_cache method.
This method is invoked when a submit button is pressed on a partial.
def clear_cache
Dir.chdir('../../../../../') #This moves up to the appropriate
directory
system('rake _0.7.3_ tmp:cache:clear')
redirect_to 'http://127.0.0.1:3000/admin’
end
The method is certainly being invoked as the redirect is working fine,
however, the method skips over the first two lines, no error messages
are appearing but the cache is not being cleared. if this method is
invoked in a seperate file in the same directory, using a call to the
method and the f5 button, the cache is clearing.
def clear_cache
Dir.chdir('../../../../../')
system('rake _0.7.3_ tmp:cache:clear')
redirect_to 'http://127.0.0.1:3000/admin’
end
clear_cache
Am pretty confused as to why in our controller the first two lines are
being skipped over and would greatly appreciate any help i recieve with
this issue.
The chdir line is almost certainly wrong - going to RAILS_ROOT would
be much more reliable.
I'm also unsure what a novice programmer is doing shelling out to a
2.5 year old version of Rake. Is there some reason why you're stuck on
0.7.3?
Hardcoding the path to the redirect is also incorrect - you'll
probably want something more like "redirect_to '/admin'", which will
work even if the app isn't running on localhost...
Finally, the entire premise here seems a little off - you haven't
mentioned what's in the cache dir that you want to clear out, but
there are much better ways of handling it in most circumstances. Take
a look at the framework docs for ActionController::Caching
(specifically, sweepers and friends).
I am new to programming and am trying to create a clear_cache method.
This method is invoked when a submit button is pressed on a partial.
You *might* want to do this via a method call to the model after the
update/save is completed.
def clear_cache
Dir.chdir('../../../../../')
system('rake _0.7.3_ tmp:cache:clear')
redirect_to 'http://127.0.0.1:3000/admin’
end
tmp:cache:clear is an awfully heavy-handed way to clear the cache. If
you smart-code your cache keys, a simple call to Rails.cache.delete from
within the model selectively dump cache fragments, whether that
'fragment' is a little bit of HTML or a whole page, just depends on your
cache key.
Your controllers can use Rails.cache.exist?(fragment_name) to avoid the
DB hit:
Your views can contain code like:
<% cache(fragment_name) do %>
regular erb or haml or whatever
<% end %>
and the models expire their own fragments via code using
Rails.cache.delete(fragment_name)