使用jquery datable gem捕获复选框分页的参数

时间:2016-03-19 09:16:48

标签: jquery ruby-on-rails-3 ruby-on-rails-4 datatables

无法捕获剩余页面的check_box参数我只获得第一页参数请帮帮我

我只能抓住提交页面参数

我的观点

private static boolean check(String input) {
    IntStream characters = input.codePoints().filter(Character::isLetter);
    return characters
            .distinct()
            .count() == characters.count();
}

我的控制器

 <table id="associations" class="table table-striped table-bordered ">
      <thead>
      <th>Check</th>
      <th>Logo</th>
      </thead>
      <tbody>
      <% @benefits.each do |benefit|%>
          <%@selected_benefits = AssocBenefit.where(:benefit_id=>benefit.id, :association_id => @association.id, :status => true).first%>
          <tr>
            <td>
              <%#= check_box_tag :car, :value => 2, :checked => (f.sex == 2) %>
              <%if @selected_benefits.present?%>
                  <%= hidden_field_tag 'benefits1[]', benefit.id %>
                  <%= check_box_tag 'benefits[]', benefit.id, :checked => (benefit.id == @selected_benefits.id)%>
                  </td>
              <%else%>
                  <%= hidden_field_tag 'benefits1[]', benefit.id %>
                  <%= check_box_tag 'benefits[]', benefit.id%></td>
              <%end%>
              <td><%= image_tag benefit.image(:thumb) %></td>
              <td><%= benefit.benefits_name%></td>

      <%end%>
      </tbody>
    </table> 

这是我的脚本

        def save_choose_benefits
   assoc_benefits = AssocBenefit.where.not(:benefit_id =>  params[:benefits]).all if params[:benefits1]
      assoc_benefits.delete_all
       unless params[:benefits].blank?

    params[:benefits].each do |d|
    ss= AssocBenefit.where(:benefit_id => d).first
    if ss.blank?
         assoc_benefits = AssocBenefit.new(:benefit_id => d ,:association_id => params[:association_id], :status=>"true")
      if assoc_benefits.valid?
        assoc_benefits.save
      end
      end
     end
      end
redirect_to associations_list_path ,:notice => "Successfully applied"

 end

我安装了gem jquery-datatables以及jquery-turbolink来传递params但是没有运气通过第二页的params等等。

请帮我解决这些问题

感谢

1 个答案:

答案 0 :(得分:0)

    <table id="associations" class=" display table table-striped table-bordered ">
      <thead>
      <th>Check</th>

      <th>Logo</th>

      </thead>
      <tbody>
      <% @benefits.each do |benefit|%>

          <%@selected_benefits = AssocBenefit.where(:benefit_id=>benefit.id, :association_id => @association.id, :status => true).first%>
          <tr>
            <td>

              <%if @selected_benefits.present?%>
                  <%= hidden_field_tag 'benefits1[]', benefit.id, :unchecked => (benefit.id != @selected_benefits.id) %>
                  <%= check_box_tag 'benefits[]', benefit.id, :checked => (benefit.id == @selected_benefits.id)  %>
                  </td>
              <%else%>
                  <%= hidden_field_tag 'benefits1[]', benefit.id %>
                  <%= check_box_tag 'benefits[]', benefit.id %></td>
              <%end%>

              <td><%= image_tag benefit.image(:thumb) %></td>
              </tr>
      <%end%>
      </tbody>
    </table>

    <%= submit_tag "Apply", :id=>"assoc_apply"%>

         <script>
          $(document).ready(function() {
              var table = $('#associations').DataTable({
              responsive: true,
              retrieve: true,
              sPaginationType: "full_numbers",
              bJQueryUI: true});

      $('#assoc_apply').click( function() {
          var data = table.$('input, select').serialize();
          var association_id = "<%=@association.id%>";
          window.location.href='/benefits/save_choose_benefits?' + data +"&association_id=" + association_id;
      } );
  } );