0

I am using the Filterrific gem to try to add filtering on an ActiveRecord object for one page only.

In the filterrific page, it says to add this line to application.js

//= require filterrific/filterrific-jquery

From what I read about the asset pipeline, this will add filterrific on all my pages. I can't have this filterrific js in the asset pipeline because it is interfering with previous written javascript on many other pages. Something in filterrific is messing up the rest of the javascript.

So to bypass this, I need to use content_for but I'm having problems making it work with javascript tag methods for a particular controller rendered page. This is what I've tried so far in my html/haml file in question.

%h1 Signed Certificates
= form_for_filterrific @filterrific do |f|
  %div
    Sorted by
    \#{f.select(:sorted_by, @filterrific.select_options[:sorted_by])}
  %div
    = link_to('Reset filters', reset_filterrific_url)
  = render_filterrific_spinner

#filterrific_results
  %div
    = page_entries_info @signed_certificates # provided by will_paginate
  %table
    %tr
      %th Common Name
      %th Effective Date
      %th Expiration Date
    - @signed_certificates.each do |sc|
      %tr
        %td= sc.common_name
        %td= sc.effective_date.strftime("%m/%d/%y %H:%M:%S")
        %td= sc.expiration_date.strftime("%m/%d/%y %H:%M:%S")
= will_paginate @signed_certificates

At the top I've tried this:

-content_for :javascript =require filterrific/filterrific-jquery
-content_for :javascript 'filterrific/filterrific-jquery'
-content_for :head do
  = javascript_include_tag 'filterrific/filterrific-jquery'

Also, I'm reading about this solution here Stackoverflow rails 3.1 but I dont think this is applicable to rails 4.2. If someone knows how to implement conditional javascript from a gem, could you please point out what I'm doing wrong?

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Dan Rubio
  • 4,709
  • 10
  • 49
  • 106
  • If you're happy to use the webpacker gem, you could add the `javascript_pack_tag` helper to include your javascript file for one particular view/haml file only. – BenKoshy Dec 04 '19 at 21:50
  • `I'm having problems` what problems? any error? something? do you have a file at /config/initializers/assets.rb? if so, it should have an array of files, add the terrific file to that list so rails compiles it as an individual linkable file – arieljuod Dec 05 '19 at 04:27

0 Answers0