title: wicked_pdf - jquery css zeba style table body only (Rails 3.2.3) and where to place css/javascript files (public or app/assets)
body: <p>This links to my previous question which was about styling a table with css. What I didn't add and realise now that this was an error is that when rendering the page as html some jquery is utilised.</p>
  
  <p><a href="https://stackoverflow.com/questions/10839778/wicked-pdf-wkhtml-support-for-css-tables-using-tr-odd-and-tr-even-to-change">wicked_pdf / wkhtml - support for css tables using tr.odd and tr.even to change background colour</a></p>
  
  <p>The jquery for this is as follows:</p>
  
  <pre><code>$(document).ready(function(){
    $('tbody tr:even').addClass('stripe');
    $('tbody tr').hover(function(){
      $(this).addClass('stripeHover');
    }, function(){
      $(this).removeClass('stripeHover');
    });
  });
  </code></pre>
  
  <p>I assume that as we are rendering a PDF file that the hover is redundant so this could be reduced to:</p>
  
  <pre><code>$(document).ready(function(){
    $('tbody tr:even').addClass('stripe');
  });
  </code></pre>
  
  <p>Here is the wicked_pdf_helper.rb file I am using. With the asset pipeline I'm not sure if I am meant to be creating my files in public, or in app/assets/javascripts. From my limitd understanding and looking on here to answer my questions + reviewing changes to the gem it seems that the changes from Rails 2 -> 3 -> 3.1 -> 3.2 have all impacted this.</p>
  
  <p>Heres the latest helper file file I have used:</p>
  
  <pre><code>module WickedPdfHelper
    def wicked_pdf_stylesheet_link_tag(*sources)
      css_dir = Rails.root.join('public','stylesheets')
      sources.collect { |source|
        "&lt;style type='text/css'&gt;#{File.read(css_dir.join(source+'.css'))}&lt;/style&gt;"
  }.join("\n").html_safe
    end
  
    def pdf_image_tag(img, options={})
      image_tag "file:///#{Rails.root.join('public', 'images', img)}", options
    end
  
    def wicked_pdf_javascript_src_tag(jsfile, options={})
      #javascript_src_tag "file:///#{Rails.root.join('public','javascripts',jsfile)}", options
    end
  
    def wicked_pdf_javascript_include_tag(*sources)
      sources.collect{ |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n").html_safe
    end
  
    module Assets
      def wicked_pdf_stylesheet_link_tag(*sources)
        sources.collect { |source|
          "&lt;style type='text/css'&gt;#{read_asset(source+".css")}&lt;/style&gt;"
    }.join("\n").html_safe
      end
  
      def wicked_pdf_image_tag(img, options={})
        image_tag "file://#{asset_pathname(img).to_s}", options
      end
  
      def wicked_pdf_javascript_src_tag(jsfile, options={})
        javascript_include_tag "file://#{asset_pathname(jsfile).to_s}", options
      end
  
      def wicked_pdf_javascript_include_tag(*sources)
        sources.collect { |source|
          "&lt;script type='text/javascript'&gt;#{read_asset(source+".js")}&lt;/script&gt;"
    }.join("\n").html_safe
      end
  
      private
  
      def asset_pathname(source)
        if Rails.configuration.assets.compile == false
          File.join(Rails.public_path, asset_path(source))
        else
          Rails.application.assets.find_asset(source).pathname
        end
      end
  
      def read_asset(source)
        if Rails.configuration.assets.compile == false
          IO.read(asset_pathname(source))
        else
          Rails.application.assets.find_asset(source).to_s
        end
      end
    end
  end
  </code></pre>
  
  <p>The previous version of the file was:</p>
  
  <pre><code>module WickedPdfHelper
    def wicked_pdf_stylesheet_link_tag(*sources)
      options = sources.extract_options!
      if request.try(:format).to_s == 'application/pdf'
        #css_dir = Rails.root.join('public','stylesheets')
        css_dir = Rails.root.join('app','assets', 'stylesheets')
        refer_only = options.delete(:refer_only)
        sources.collect { |source|
          source.sub!(/\.css$/o,'')
          if refer_only
            stylesheet_link_tag "file://#{Rails.root.join('public','stylesheets',source+'.css')}", options
          else
            "&lt;style type='text/css'&gt;#{File.read(css_dir.join(source+'.css'))}&lt;/style&gt;"
          end
        }.join("\n").html_safe
      else
        sources.collect { |source|
          stylesheet_link_tag(source, options)
        }.join("\n").html_safe
      end
    end
  
    # def wicked_pdf_stylesheet_link_tag(*sources)
    #   sources.collect { |source|
    #     "&lt;style type='text/css'&gt;#{Rails.application.assets.find_asset("#{source}.css")}&lt;/style&gt;"
    #   }.join("\n").gsub(/url\(['"](.+)['"]\)(.+)/,%[url("#{wicked_pdf_image_location("\\1")}")\\2]).html_safe
    # end
  
    def pdf_image_tag(img, options={})
      if request.try(:format).to_s == 'application/pdf'
        image_tag "file://#{Rails.root.join('app', 'assets', 'images', img)}", options rescue nil
      else
        image_tag img.to_s, options rescue nil
      end
    end
  
    def wicked_pdf_javascript_src_tag(jsfile, options={})
      if request.try(:format).to_s == 'application/pdf'
        jsfile.sub!(/\.js$/o,'')
        javascript_src_tag "file://#{Rails.root.join('public','javascripts',jsfile + '.js')}", options
      else
        javascript_src_tag jsfile, options
      end
    end
  
    def wicked_pdf_javascript_include_tag(*sources)
      options = sources.extract_options!
      sources.collect{ |source| wicked_pdf_javascript_src_tag(source, options) }.join("\n").html_safe
    end
  end
  </code></pre>
  
  <p>The template (genie_help_base.pdf.erb) file is as follows:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;&lt;%= full_title(yield(:title)) %&gt;&lt;/title&gt;
      &lt;%#= wicked_pdf_stylesheet_link_tag    "application", :refer_only =&gt; true %&gt;
      &lt;%#= wicked_pdf_stylesheet_link_tag "application", :refer_only =&gt; true %&gt;
      &lt;%= wicked_pdf_stylesheet_link_tag "static_pages/genie_v23_help" %&gt;    
      &lt;%= javascript_include_tag "application" %&gt;
      &lt;%= csrf_meta_tags %&gt;
  &lt;!--     &lt;%#= render 'layouts/shim' %&gt;  --&gt;   
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div class="container"&gt;
        &lt;%= yield %&gt;
        &lt;%= debug(params) if Rails.env.development? %&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The layout template for html (genie_v23_help_base.html.erb) is:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;&lt;%= full_title(yield(:title)) %&gt;&lt;/title&gt;
      &lt;%= stylesheet_link_tag    "application", media: "all" %&gt;
      &lt;%= javascript_include_tag "application" %&gt;
      &lt;%= csrf_meta_tags %&gt;
      &lt;%= render 'layouts/shim' %&gt;    
    &lt;/head&gt;
    &lt;body&gt;
      &lt;%= render 'layouts/header' %&gt;
      &lt;div class="container"&gt;
        &lt;%= render 'static_pages/genie_v23_help/shared/help_header.html.erb' %&gt;       
        &lt;%= yield %&gt;
        &lt;%= render 'static_pages/genie_v23_help/shared/help_footer' %&gt;
        &lt;h4&gt;&lt;strong&gt;Did this show up in only the html on screen&lt;/strong&gt;&lt;/h4&gt;
        &lt;%= render 'layouts/footer' %&gt;
        &lt;%= debug(params) if Rails.env.development? %&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is the css file</p>
  
  <pre><code>table {
    width: 90%;
    border: 1px solid #999999;
  }
  
  th, td {
    padding: 7px 10px 10px 10px;
  }
  
  th {
    border-bottom: 2px solid #111111;
    border-top: 1px solid #999999;
    font-size: 90%;
    letter-spacing: 0.1em;
    text-align: left;
    text-transform: uppercase;    
  }
  
  /*tr.even {
      background-color: #efefef;
  }*/
  
  
  .stripe { 
    background-color: #dddddd;
    /*color: #666666;*/
  }
  
  tr.stripeHover {
    background-color: #FFFACD;
  }
  
  tbody tr:hover {
      background-color: #FFFACD;
  }
  </code></pre>
  
  <p>Finally the show method in my controller where this is all called from:</p>
  
  <pre><code>  def static_page
      respond_to do |format|
      format.html do
          render :template =&gt; show_page,
                 :layout =&gt; 'genie_v23_help_base'
      end
      format.pdf do
          render :pdf =&gt; show_page,
                 :layout =&gt; 'genie_v23_help_base.pdf.erb',
                 :template =&gt; "#{show_page}.html.erb",
                 :handlers =&gt; :erb,
                 :disable_external_links =&gt; true,
                 :disable_javascript =&gt; false,
                 :show_as_html =&gt; params[:debug],
                 :print_media_type =&gt; true
        end
      end
    end
  </code></pre>
  
  <p>So I at present cannot get the jquery striping function to work.</p>
  
  <p>I want to get this working and if successful will most likely build a sample type app to put up on GitHub as there doesn't seem to be an up-to-date version with Rails 3.2.3 using the stylesheets and jquery to demonstrate this.</p>
  
  <p>Do I need to use an include statement in a file somewhere? If so where 'application'?</p>
  
  <p>With the update to the helper (tobiashm 18 days ago Use precompiled assets if needed) how does one specify whether its the 'regular' methods or the methods in the Asset module?
  If I don't comment out the following I get an error.</p>
  
  <pre><code>def wicked_pdf_javascript_src_tag(jsfile, options={})
    #javascript_src_tag "file:///#{Rails.root.join('public','javascripts',jsfile)}", options
  end
  </code></pre>
  
  <p>Changed the javascript_src_tag to javascript_include_tag so compliant with Rails 3.2+ </p>
  
  <pre><code>def wicked_pdf_javascript_src_tag(jsfile, options={})
    javascript_include_tag "file:///#{Rails.root.join('public','javascripts',jsfile)}", options
  end
  </code></pre>
  
  <p>Does anyone have a hosted sample app /app demonstrating all of this?</p>
  
  <p>Summary - trying to get css zebra striping in a PDF using jquery to set the class name in the html rendered file, so the css styling can be applied.</p>
  
  <p>Thanks in advance, Mark</p>