作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用过 will_paginate gem 用于对我的 test_app 的索引页进行分页。它只显示列出的拍卖,并与每个拍卖相对应,有编辑、销毁功能。我的分页运行良好。我总共以表格方式列出了 6 个拍卖,当我放置 per_page = 2 时,有 3 页。但是当我在第 2 页或第 3 页编辑和更新拍卖时,它只会在更新操作后转到第一页。我希望它保持在同一页面上。
我的观点 - Index.html.erb
<h1>Listing auctions</h1>
<table>
<tr>
<th>Name</th>
<th>Category</th>
<th>Item</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @auctions.each do |auction| %>
<tr>
<td><%= auction.name %></td>
<td><%= auction.category %></td>
<td><%= auction.item_id %></td>
<td><%= link_to 'Show', auction %></td>
<td><%= link_to 'Edit', edit_auction_path(auction) %></td>
<td><%= link_to 'Destroy', auction, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= will_paginate @auctions %>
<%= link_to 'New Auction', new_auction_path %>
<%= link_to 'Back to Home', home_path %>
class Auction < ActiveRecord::Base
attr_accessible :name, :category, :item_id
self.per_page = 2
end
class AuctionsController < ApplicationController
# GET /auctions
# GET /auctions.json
def index
@auctions = Auction.paginate(:page => params[:page])#pagination for Index
#@posts = Post.paginate(:page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @auctions }
end
end
# GET /auctions/1
# GET /auctions/1.json
def show
@auction = Auction.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @auction }
end
end
# GET /auctions/new
# GET /auctions/new.json
def new
@auction = Auction.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @auction }
end
end
# GET /auctions/1/edit
def edit
@auction = Auction.find(params[:id])
end
# POST /auctions
# POST /auctions.json
def create
@auction = Auction.new(params[:auction])
respond_to do |format|
if @auction.save
format.html { redirect_to @auction, notice: 'Auction was successfully created.' }
format.json { render json: @auction, status: :created, location: @auction }
else
format.html { render action: "new" }
format.json { render json: @auction.errors, status: :unprocessable_entity }
end
end
end
# PUT /auctions/1
# PUT /auctions/1.json
def update
@auction = Auction.find(params[:id])
respond_to do |format|
if @auction.update_attributes(params[:auction])
format.html { redirect_to @auction, notice: 'Auction was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @auction.errors, status: :unprocessable_entity }
end
end
end
# DELETE /auctions/1
# DELETE /auctions/1.json
def destroy
@auction = Auction.find(params[:id])
@auction.destroy
respond_to do |format|
format.html { redirect_to auctions_url }
format.json { head :no_content }
end
end
end
最佳答案
您需要保留参数[:page] 在整个编辑/更新操作中并将其与您的查询字符串合并
关于ruby-on-rails - Rails 中的分页 - 在编辑/更新属于其他页面的元素后始终重定向到第一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064553/
我是一名优秀的程序员,十分优秀!