javascript - How to include js.erb file in view folder -
i have javascript file use view. there needs ruby code in it, , need render
in ruby, understand can't put javascript file in asset pipeline. can put in same view folder .html.erb
file.
how include javascript file, or use javascript file view file? tried javascript_include_tag
in view (that uses asset pipeline apparently), using script src="myfile.js"
myfile.js.erb
file (but can't find myfile.js
), , names js.erb
file (users.js.erb
) same .html.erb
file (users.html.erb
), no avail.
javascript_include_tag won't work js.erb declared in view folder itself. there 3 different ways can have javascript.
1] write code in view, i.e., in html.erb itself.
2] create js file in public/javascripts folder , include using javascript_include_tag.
3] in case want make request ajax:
- create js.erb in view folder same name of action.
- in view form created calling action, make request using
:remote => true
. - in called action, use code follows,
def action
respond_to |format|
format.js
end
end
Comments
Post a Comment