Reputation: 7280
I am a rookie in RoR. I understand the MVC model. The html.erb files are responsible for rendering the view. But what do the the js.erb files do? Are they related only to any jquery that may be present in the view or do they contribute to the overall view as well. Please bear with my naivity.
Thanks
Upvotes: 1
Views: 533
Reputation: 1798
JS.ERB (ERB: embedded Ruby) files are standard javascript files. They reside within the view, or app/assets which contains custom JS applicable to the application.
If the JS.ERB file is located with the view folder, it can be referenced by the controller and contain any kind of Ruby on Rails based code.
If the JS.ERB is located with the app/assets folder, it can only reference Ruby code related to the Asset Pipeline, i.e:
<%= asset_tag 'image.png' %>
This image would reside within app/image, in order for the asset_tag to retrieve the image.
Upvotes: 2
Reputation: 5015
ERB is short for embedded ruby, anything with that extension can have ruby code written in it. In rails this is used to dynamically create html, however you can also dynamically create JS files as well. Imagine you had a js.erb file with the following content
<% [1,2,3,4,5].each do |i| %>
alert(<%= i %>);
<% end %>
This would render the following javascript:
alert(1);
alert(2);
alert(3);
alert(4);
alert(5);
Which would result in 5 alerts being shown to the client (not a good idea in practice, but I hope you get the idea)
Upvotes: 0
Reputation: 5929
Your controller's action will respond with js.erb
when you do a javascript request. For example ajax request via:
link_to '', users_path, remote: true
or
form_for @user, remote: true
Upvotes: 0