Rails 3 caching: expired action for named route

My controller has the following:

caches_action :render_ticker_for_channel, :expires_in => 30.seconds 

In my routes file, I have the following:

 match '/render_c_t/:channel_id' => 'render#render_ticker_for_channel', :as => :render_channel_ticker 

In the log file, I see the following:

 Write fragment views/mcr3.dev/render_c_t/63 (11.6ms) 

How do I finish this manually? I need to finish this with a controller other than the render controller, but even inside the render controller I cannot make it expire the right thing.

If I do this:

  expire_action(:controller => 'render', :action => 'render_ticker_for_channel', :id => c.id) 

I see:

 Expire fragment views/mcr3.dev/render/render_ticker_for_channel/63 (3.2ms) 

If I do this:

 expire_action(:controller => 'render', :action => 'render_c_t', :id => c.id) 

I see:

 Expire fragment views/mcr3.dev/render/render_c_t/63 (3.2ms) 

It:

 expire_action("render_c_t/#{c.id}") 

gives:

 Expire fragment views/render_c_t/63 (3.5ms) 

How can I make it expire the same path that generates 'caches_action' ?!

+6
source share
3 answers

Use the regex version of expire_fragment :

 expire_fragment %r{render_c_t/#{c.id}/} 
+5
source

There must be more β€œRails Way” for this, but it can work as a backdoor: Rails.application.routes.url_helpers will give you access to your helpers, and each will return a line in which the path and / or URL that will be Sent to the browser in the Location header.

Try the Rails.application.routes.url_helpers.render_ticker_for_channel_path(63) , which should return /render_c_t/63 and Rails.application.routes.url_helpers.render_ticker_for_channel(63, :host => 'mcr3.dev') , which should return http://mcr3.dev/render_c_t/63

With some manipulation, you can split this second line to return to the name that Rails uses for the cached action:

 def funky_action_cache_name(route, params) Rails.application.routes.url_helpers.send(route.to_s+'_url', params).gsub(/https?:\/\//,'') end # expire_action(funky_action_cache_name(:render_ticker_for_channel, :id => 63)) 

Not the prettiest solution, but it should work!

0
source
 caches_action :render_ticker_for_channel, :if => proc do !!params['doCache'] end 

But for this solution, we need to pass an additional parameter either through the query string or into the body of the message.

-1
source

Source: https://habr.com/ru/post/895132/


All Articles