how to manually include remotipart js?

100 Views Asked by At

I need to conditionally use the remotipart gem. The [docs][1] say just add it to application.js:

//= require jquery.remotipart

But I don't want it to be included with every single view, instead I want to conditionally include it, however when I try:

<%= javascript_include_tag "jquery.remotipart" %>

I get an error. How do i reference a js included as part of a gem generically, and remotipart js specifically?

Thanks, Kevin

1

There are 1 best solutions below

0
On

But I don't want it to be included with every single view, instead I want to conditionally include it, however when I try:

What means conditionally in this context? The most simplest way would be

<%= javascript_include_tag "jquery.remotipart" if condition %>

Also you could use content_for like this on the views where you want to include it:

# in your view
<% content_for :js do %>
  <%= javascript_include_tag "jquery.remotipart" %>
<% end %>

# in your layout.html.erb
<%= yield :script %>

https://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for

If you meant that you get an error that the JS file can't be found, please update your question with what library (webpacker, sprockets) and Rails version you use.