加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails中的分页 – 在编辑/更新属于其他页面的

发布时间:2020-12-17 03:36:34 所属栏目:百科 来源:网络整理
导读:我使用了will_paginate gem来为我的test_app分页索引页面.它只显示列出的拍卖和每个拍卖对应的编辑,销毁功能.我的分页工作得很好.我总共有6个拍卖会以表格方式列出,因为我把per_page = 2,就有3页.但是当我编辑更新第2页或第3页的拍卖,它只是在更新操作后转到
我使用了will_paginate gem来为我的test_app分页索引页面.它只显示列出的拍卖和每个拍卖对应的编辑,销毁功能.我的分页工作得很好.我总共有6个拍卖会以表格方式列出,因为我把per_page = 2,就有3页.但是当我编辑&更新第2页或第3页的拍卖,它只是在更新操作后转到第1页.我希望它能保持在同一页面上.

我的观点 – 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 %>

我的模特 – Auction.rb

class Auction < ActiveRecord::Base
  attr_accessible :name,:category,:item_id
  self.per_page = 2
end

我的控制器 – auctions_controller.rb

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

请提出解决方案.谢谢.

解决方法

在您的更新操作中,您有拍卖ID,您可以在那里计算预期的页面编号.相应地使用
redirect_to @auction,:page => page_no

或者你必须将页面参数从节目中发送到编辑页面(当点击编辑链接时),然后再到更新操作以使用该页面来重定向

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读