我用过will_paginate宝石。我有一个用于选择用户的复选框。每页显示5个用户。当我翻页时,第一个选定的复选框值不会持久。
//实施代码 new.html.erb
<td><%= check_box_tag 'users[]',x.id, checked(@team,x)%></td>
//控制器代码
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:5)
end
// log
`Started GET "/teams/3" for 127.0.0.1 at 2019-03-08 13:15:03 +0530
Processing by TeamsController#show as HTML
Parameters: {"id"=>"3"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 81], ["LIMIT", 1]]
↳ /Users/beyondapplications/.rvm/gems/ruby-2.4.5/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Rendering teams/show.html.erb within layouts/application
Team Load (0.6ms) SELECT "teams".* FROM "teams"
↳ app/views/teams/show.html.erb:3
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 87], ["LIMIT", 1]]
↳ app/views/teams/show.html.erb:7
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 90], ["LIMIT", 1]]
↳ app/views/teams/show.html.erb:7
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 89], ["LIMIT", 1]]
↳ app/views/teams/show.html.erb:7
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 80], ["LIMIT", 1]]
↳ app/views/teams/show.html.erb:7
Rendered teams/show.html.erb within layouts/application (11.3ms)
Rendered layouts/_header.html.erb (1.9ms)
Completed 200 OK in 231ms (Views: 222.4ms | ActiveRecord: 1.9ms)
`