user1130176
user1130176

Reputation: 1858

how to manually include remotipart js?

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

Upvotes: 0

Views: 100

Answers (1)

Christian Bruckmayer
Christian Bruckmayer

Reputation: 2197

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.

Upvotes: 0

Related Questions