Home > Software engineering >  Load Javascript File for Ajax (js.erb)
Load Javascript File for Ajax (js.erb)

Time:02-10

For my current design, I have 3 menus that load different content, so I decided to use Ajax render and js.erb files.

An example of this:

controller:

def my_route
  respond_to do |format|
    format.js
  end
end

my_route.js.erb:

$('#content').html("<%= j render 'my_route_partial' %>");

This loads my page, but in the main_page which loads the three Ajax content, I have:

<% content_for :javascripts do %>
  <%= include_javascript_tag 'my_javascript_file' %>
<% end %>

This loads the javascript related to the main page, but it does not load the javascript that should work with any of the pages loaded through Ajax afterwards.

I have also tried to just include the javascript tag into the files, but it didn't work. I don't want to leave the javascript code into the js.erb file, so is there any way to do it?

Thanks in advance.

CodePudding user response:

  •  Tags:  
  • Related