0

I'm tryng to use a plugin called DataTable, so, I need to use JQuery in my application, but I wanna allow javascript just in one page.

So, in my folder layout, I have:

--app
---layout
-----application.html.erb
-----user_reports.html.erb

In application, I have my layout used for all pages, something like:

<!DOCTYPE html>
<html>
<head>
<%= render 'layouts/head' -%>
</head>
<body class="gray-bg">

<div id="modal" class="modal hide"></div>
<div class="main-header">
  <div class="container">
    <% if show_title? -%>
      <h1>
        <%= content_for(:title) -%>
        <% if show_subtitle? -%>
          <small><%= content_for(:subtitle) -%></small>
        <% end -%>
      </h1>
    <% end -%>

    <% if content_for?(:page_actions) -%>
      <div class="page-actions">
        <%= content_for(:page_actions) -%>
      </div>
    <% end -%>
  </div> <!-- /container -->
</div> <!-- /main-header -->

<div class="container">
  <div class="page-content">
    <div id="alert_message"><%= render 'layouts/alerts' -%></div>
    <%= yield -%>
  </div> <!-- /page-content -->
</div> <!-- /container -->

<footer class="no-border">
</footer>

<%= yield(:foot) -%>

To use JQuery in a page, I created the file user_reports.html.erb inside layouts:

<%= yield %>

<%= yield :javascript %>

In my view, I have a table and a script:

<table id="table" class="zebra-striped">
  <thead>
    <tr>
      <th>#</th>
      ....
    </tr>
  </thead>
  <tbody>
    ....
  </tbody>
</table>

<% content_for :javascript do %>
  <%= javascript_include_tag "jquery.min.js" %>
  <%= javascript_include_tag "datatables/js/jquery.dataTables.min.js" %>

  <script>   
    $("table").dataTable({
      sPaginationType: "full_numbers",
      iDisplayLength: 10,
      aLengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
      oLanguage: {
        sUrl: "/javascripts/datatables/data_tables_lang.json"
      }
    });
  </script>

<% end %>

The problem is the layout, when I'm using the file created:

a

When I'm not, the layout is fine, but javasript not works.

b

I wanna use JQuery only in this page without change my layout, how can I to do this?

My version of rails is: 3.0.10

Please, sorry my english =]

Lorena Bento
  • 544
  • 10
  • 29
  • can you show your application.js file? and please be more clear what you actually want – Animesh May 09 '14 at 14:14
  • what are you hoping to gain by just using jQuery on one page? the asset pipeline will load everything on the first request, so you can then use it throughout the site..If your worried about performance then i dont think thats going to be an issue – Richlewis May 12 '14 at 07:35

0 Answers0