Problem with method that performs a rake

hello,

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.

regards,
mike

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).

–Matt J.

On Aug 11, 1:33 pm, Michael W. [email protected]

Michael W. wrote:

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

Mike, they are probably not being skipped but your system call is
erroring out due to not being in the place you think you are…

The relative addressing you are using above looks rather fragile… any
way to use the context path?

use the log to output the Dir.pwd … or make sure rake is in your path…

lastly, I believe you can run rake from within ruby which is probably a
better alternative than to use the system…

ilan

Michael W. wrote:

hello,

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)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs