I have a file under app/assets/javascripts/templates.js.erb which loads templates from other html views.
= javascript_include_tag "templates.js"
In development mode, my templates are changing very often so templates.js.erb should be re-processed at every page load. But it is cached unless the code in the file changes.
How can I force rails to re-execute templates.js.erb at every page request in development mode?
you can simply use your js code inside <script> tag in layout or particular views for reloading it
Thanks –
you can check in controller wether the request is ajax or not by checking request.xhr? so you
can chached your action conditionally
for example
caches_page :show, :if => lambda { !request.xhr? }
Related
I have a situation in Rails 5.2 where a controller is rendering a .js.erb file to the view literally, i.e. showing the javascript code in the view itself.
How can I get Rails to run the js.erb file instead of showing it as if it were a DOM file? It is taking the user away from a page I want the user to stay on.
# controller
def submit_custom_data
...perform some functions...
respond_to do |format|
format.js
end
end
# submit_custom_data.js.erb
console.log('submit_custom_data...');
... Other javascript ...
Additional Info
By preserving the browser console logs on Chrome, I see this:
Resource interpreted as Document but transferred with MIME type text/javascript: "http://localhost:3000/submit_custom_data/1068.js?c=1&d=10&db=1&s=8&tm=&w=3".
Navigated to http://localhost:3000/submit_custom_data/1068.js?c=1&d=10&db=1&s=8&tm=&w=3
This issue did NOT occur on Rails 4.2.7. After updating to Rails 5.2.7 (and updating the bundle), I'm now seeing these issues.
In order to boost the performance of my Rails 4.0.2 app, I would like to cache the output of some of my static pages:
class PagesController < ApplicationController
def home
end
def about_us
end
def contact
end
end
In the Rails Guide on Caching it says that "Page Caching has been removed from Rails 4" and moved into a gem. In the gem description it says, however, that it will be maintained only until Rails 4.1. Some other observers also advise against using Page Caching and endorse Russian doll caching instead.
So what's the best way to cache a bunch of static pages that will never actually hit the database and only ever change (slightly) if a user signs in?
Thanks for any suggestions.
You can still using fragment caching for your static pages, although the benefits are obviously more visible with dynamic / DB-driven pages. It's worth considering doing this should you have a lot of partial being rendered or costly view logic. Just wrap your page's template with:
# about_us.html.erb
<% cache 'about_us' do %>
...
<% end %>
the first time you hit the page in an environment where config.action_controller.perform_caching = true, it'll generate the fragment (which in this case is your whole page), and it'll serve that the next time you reload it. The cache digest will be invalidated when the template is changed:
The template digest that's added to the cache key is computed by
taking an md5 of the contents of the entire template file. This
ensures that your caches will automatically expire when you change the
template file.
http://api.rubyonrails.org/classes/ActionView/Helpers/CacheHelper.html
Is there a way to open .html.erb files by themselves in the browser. The Rails app I am trying to have a look at is in rails 2.3 and will not run locally on the rails server nor will it run when I give the command script/server.
I would just like to open the individual views in the browser by themselves...is this possible?
You can open them in the browser, but the <% %> and <%= %> tags will get shown as-is instead of executing the Ruby code. These are template files that need to get processed by Rails to fill in data at runtime. Your best bet is TsaiKoga's answer, making a controller action render the template in question.
In rails4 you can do it like this:
erb a.html.erb > a.html
Maybe it doesn't work in rails 2.3. If you want to render that view, maybe you can use an action just like users#index and change the code to render it:
render "devise/mailer/a.html.erb", :layout => false
How would this be updated for Rails 3.1?
http://railscasts.com/episodes/88-dynamic-select-menus
I just can't figure out how to call the js.erb file and have it run the code to generate the javascript dynamically.
Might be something: in Rails 3.1, you're most likely using jQuery instead of Prototype. The example code on the Railscasts site is using good old Prototype instead of the new hotness that is jQuery (default javascript library in Rails 3.1).
Once all your jquery pipes are connected, having rails respond to and render your js.erb is the same as always. In your controller:
def country_selected
// whatever you need to do
respond_to do |format|
format.js
end
end
Then in your view directory, you have a country_selected.js.erb that you can put in whatever javascript you want to update the second select menu. (Remember you have to escape your shiz for it to work correctly) e.g.
<%= escape_javascript(params[:country]) %>
By the way, I think .rjs was moved out of Rails proper and into it's own Gem. Something else to keep in mind regarding Rails 3.1 vs. javascript.
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( ... )