如何在分页中持久保存复选框的值

时间:2019-03-06 11:30:10

标签: ruby-on-rails

在Rails应用程序中,使用will_paginate gem我正在对用户进行分页,每个用户都有一个复选框来选择他们以组成一个团队,现在我的问题是在分页时,上一页中检查的值未提交给模型

我当前实现的代码如下

// new.html.erb

<div class="col-md-6 col-md-offset-3">
  <div class="container">
    <br>
    <h3>Add players to team <%= @team.team_name %></h3>
      <%= form_for @player do |f| %>

        <table>
        <tbody>
        <%= f.hidden_field :team_id, :value=> @team.id %>
          <tr>
            <th>Player Id</th>
            <th>Player Name</th>
            <th>Select</th>
          </tr>  
          <% @user.each do |x| %>
            <tr>
              <td><%= x.id %></td>
              <td><%= x.full_name %></td>
              <td><%= check_box_tag 'users[]',x.id,checked(@team,x) %></td>
           </tr>
        <% end %>
        <tr>   
          <td>   
            <%= f.submit nil, class: 'btn btn-primary' %>
            <%= link_to t('.cancel', default:t("helpers.links.cancel")),
                root_path, class:'btn btn-default' %> 
           </td> 
          </tr>
        </tbody>
      </table> 
    <% end %>
  </div>
</div>
<%= will_paginate @user,renderer: BootstrapPagination::Rails,previous_label:"old",next_label: "new",inner_window:1,outer_window:1 %>

//players_controller


 def new
        @player= Player.new
        @team = Team.find(params[:team_id])
        @players=User.includes(:roles).where(roles:{name:'Player'})
        @user = @players.all.paginate(page: params[:page], per_page:2)
    end

0 个答案:

没有答案