简体   繁体   中英

How do I selectively turn off template caching in Rails?

I am sending emails in a background job using ActionMailer. Users can create new email templates but they aren't recognized until the background job is restarted. Used to use

ActionView::TemplateFinder.reload!

which forced reloading of templates (now deprecated on 2.3.4). I have tried

ActionView::Base.cache_template_loading = false

but that does not work.

What I wound up doing was setting a global variable in my background process before the Rails environment was loaded:

$background = true

then in environments/production.rb:

config.action_view.cache_template_loading = !$background

Not thrilled, but it works. I get template reloading for email templates in my background job but cached view templates for the online application.

Since your users can create (and possibly change) templates, why don't you store them on the database and render as inline erb?

render :inline => template_record.contents

Now that I suggested this, I noticed... You can also use :inline to manually read the template and pass it to ActionView. You'll have to handle the exceptional case of the template not existing, though.

render :inline => File.read( ... )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM