0

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

user1130176
  • 1,772
  • 1
  • 23
  • 33
  • "But I don't want it to be included with every single view, instead I want to conditionally include it". If you're using turbolinks thats just a straight up bad idea as it creates a persistent browser session across many page loads. Gemified assets is also an outdated concept if you are using Rails 6 with webpacker. Use yarn packages instead and leave the idea of running different js on different pages behind. – max Sep 17 '20 at 12:15

1 Answers1

0

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.