.js.erb VS .js

后端 未结 3 1695
梦如初夏
梦如初夏 2020-12-01 00:46

What is the advantage to putting you javascript for your rails app into a .js.erb file instead of just throwing it in your application.js file? I have a create button for bu

3条回答
  •  心在旅途
    2020-12-01 01:18

    Per my view, the reason to have js.erb files is to gain access to rails helpers like the something_url routes. I've found, most of the time, that those URLs are already embedded in the

    tag or something like that, so I have little need for it. Processing everything with .erb also prevents you from caching javascript as aggressively.

    For those reasons, I think the most appropriate decision is to put javascript in the most unobtrusive place possible: application.js (or other static scripts). Especially with jQuery, that's really as it was intended.

    By the way, javascript_auto_include is a nice, tidy plugin for organizing your javascript according to the views that are rendered. Rather than stashing everything in application.js, you might consider this.

    js.erb versus js.rjs

    One of these two (.erb) is really for generating javascript from a template. This is the sort of thing you would include as a tag in the HTML. The other (.rjs) is about controlling page content in an AJAX response, and would not be run until such an AJAXy call was made.

提交回复
热议问题