How to include js.erb file in view folder
javascript_include_tag won't work js.erb declared in the view folder itself. There are three different ways you can have the javascript.
1] Write the code in the view, i.e., in the html.erb itself.
2] Create js file in public/javascripts folder and include it using javascript_include_tag.
3] In case you want to make the request as Ajax:
- Create the js.erb in the view folder itself with the same name as that of the action.
- In the view where some form is created which will be calling this action, make the request using
:remote => true
. In the called action, use code as follows:
def action
respond_to do |format|
format.js
end
end
Using erb render in javascripts folder
Since assets cannot use render method, move your .js.erb files into views directory and use
<%= j render(:partial => 'accounts/newbutton') %>
Reference - Rendering partial in js.erb file and https://stackoverflow.com/a/6309970/184184
Organizing .html.erb and .js.erb files within a view directory
You could say to your action where exactly template is
respond_to do |format|
format.js { render "blogs/js/filter_by_team" }
end
And place js template into views/blogs/js
How do I use javascript_include_tag with js.erb file in Rails 4?
The file should be named assets/javascripts/social_sharing.js.erb
(with .erb on the end), otherwise the Rails asset pipeline won't know to process it and will serve it up as if it were plain JavaScript. The include tag should read <%= javascript_include_tag "social_sharing.js" %>
rather than .erb
, although the .js is optional.
Everything in the assets folder will get processed through the asset pipeline and can be easily pulled in with asset helper tags.
HOWEVER, javascript_include_tag
and the asset pipeline are for truly static assets that will only be precompiled once and then the same file served with every relevant request. If you're trying to include dynamic content such as <%= post.id %>
that will vary from request to request, you will probably want to move the .js.erb file to somewhere in the views directory and render it as a partial, similar to the first answer for this question:
<script>
render partial: '[somewhere]/social_sharing', post: @post
</script>
The file will need to be renamed _social_sharing.js.erb
as well.
Include same js file in multiple html.erb files in Rails
No, normally the javascript code is not available in the other views, just in the one where you include it. So you need to include it again.
The exception would be if you have it in a layout file like application.html.erb
because this on is rendered in all views.
If you call the javascript package in both partials anyway, then you can also just call it in main, after the if statement. This will be loaded after the partials have been loaded. It might seem weird, to require a piece javascript in a file that doesn't seem to have the html elements that you are using in the JS file, however, the partials are just a way to organise the code in rails. In the end they will be merged into one big html file.
In the comments you said this if statement is nested into another if statement, so maybe you don't need the javascript at all? Paste it at the end of the if statement like so
<% if outer_if_statment %>
<% if some_condition %>
<% render partial: 'first_partial' %>
<% elsif some_other_condition %>
<% render partial: 'second_partial' %>
<% else %>
<% render partial: 'first_partial' %>
<% render partial: 'second_partial' %>
<% end %>
<%= javascript_include_tag 'common' %>
<% else %>
...
<% end %>
This way it will be available in for all partials but not, if the inner if statement is not chosen.
Another thing:
Javascript should be loaded at the end of the html file anyway so that the user doens't have to wait for it to load while he/she doesn't see any html elements. If we put the javascript right in the middle of a hmtl file like this, we're infringing this principle.
Now there is a helper to that:
You can define a second yield
in your application.html.erb
<body>
<%= yield %>
<%= javascript_include_tag 'application' %>
<%= javascript_pack_tag 'application' %>
<%= yield(:after_js) %>
</body>
In the views, you can call it like this:
<%= content_for :after_js do %>
<%= javascript_include_tag 'common' %>
<% end %>
This will make sure, that the JS is loaded at the end of the body and that all other JS code is loaded beforehand (if there are any dependencies).
And, if you still only want to load it depending on the conditions you could repeat the if statement inside the content_for
block.
Depending on what your JS script looks like, you can also make sure it only executes, if the necessary HTML element is found on the page. Let's imagine you want to add an event listener on a button.
// common.js
const button = document.querySelector("#red-button")
if (button) {
button.addEventListener(...)
}
Ruby on Rails View inserting js.erb file via render
Nice :)
I think I found the answer on my own with this SO article
= render(:partial => 'inserts_js_enhanced_element', :handlers => [:erb], :formats => [:js])
Works, and doesn't give me the Deprecation warning
Rails: js file in view folder isn't getting called
*.js.erb files are rendered when you are using AJAX/JS with your controller actions. By default, when you call the create method, Rails will respond using HTML. This will load a new page. Sometimes you want to use AJAX instead, and that's why you create js.erb files in the view folders.
For this to work, the form and/or link_to objects you are using must be AJAX enabled (they should have a :remote => true
attribute on them) If they are not specified as remote forms, they will execute the HTML instead of the JS and the create.js.erb file will never be called.
Your controller method also needs to know how to respond to js requests. You need something like:
respond_to do |format|
format.js
end
That code tells Rails to look for a file called "method".js.erb in your view folder, so in this case, create.js.erb.
These files are completely different from regular JS files you put in the asset pipeline -- these are view templates to be rendered as the result of a controller action.
You might find some Rails/AJAX tutorials helpful...here's a pretty good one that walks you through this whole process:
http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3
Hope that helps, if you need more assistance please post the code for your controller and any of the view files...
Is it possible to specify what js.erb file I want to run?
Rails: Render a .js.erb from another controller?
format.js { render :file => "/users/feed.js.erb"}
Related Topics
How to Show Modal Window in Controller's Action
Angular 2: Two Backend Service Calls on Success of First Service
Why Does "True" == True Show False in JavaScript
Set Default Value of JavaScript Object Attributes
Parsing JSON Giving "Unexpected Token O" Error
How to Programmatically Click on an Element in JavaScript
Access Elements of Parent Window from Iframe
Double Negation (!!) in JavaScript - What Is the Purpose
Meteor: Calling an Asynchronous Function Inside a Meteor.Method and Returning the Result
Wkwebview - Complex Communication Between JavaScript & Native Code
In JavaScript How Do I/Should I Use Async/Await with Xmlhttprequest
Encrypt iOS and Decrypt Node.Js Aes
Uint8Array to String in JavaScript
Can Jquery .Keypress() Detect More Than One Key at the Same Time
Ruby on Rails 4 JavaScript Not Executed
JavaScript Parse Float Is Ignoring the Decimals After My Comma