我需要確保同時在頁面上顯示不超過 24 種產品,如果其中超過 24 種,則應該有一個按鈕,將我重定向到第二頁的產品。
也有必要, 當去下一頁, 它以某種方式反映在網站的鏈接, 即在第一頁的鏈接應該是 / 產品?PageIndex = 1, 在第二 / 產品上?PageIndex =
2 這是我的索引.html
<h1>Products</h1>
<table >
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><button type = 'button' class="btn btn-outline-info"><%= link_to 'Show', product %></td>
<td><button type="button" class="btn btn-outline-success"><%= link_to 'Edit', edit_product_path(product) %></td>
<td><button type="button" class="btn btn-outline-danger"><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<button><%=link_to 'Destroy All', delete_all_products_path, method: :delete, data: { confirm: 'Are you sure?' } %></button>
<br>
<%= link_to 'New Product', new_product_path %>
這是我的控制器
class ProductsController < ApplicationController
before_action :set_product, only: %i[ show edit update destroy ]
# GET /products or /products.json
def index
@products = Product.all
end
# GET /products/1 or /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
def count
end
def expensivest
if Product.count > 0
@product = Product.order(price: :desc)[0]
else redirect_to products_url, notice: "Product list is empty"
end
end
def median
if Product.count > 0
sorted = Product.order(price: :asc)
median = sorted.length / 2
@product = sorted[median]
else redirect_to products_url, notice: "Product list is empty"
end
end
def cheapest
if Product.count > 0
@product = Product.order(price: :asc)[0]
else redirect_to products_url, notice: "Product list is empty"
end
end
# POST /products or /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: "Product was successfully created." }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1 or /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: "Product was successfully updated." }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1 or /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: "Product was successfully destroyed." }
format.json { head :no_content }
end
end
def delete_all
Product.delete_all
redirect_to products_url, notice: "Product was successfully destroyed."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Only allow a list of trusted parameters through.
def product_params
params.require(:product).permit(:name, :price)
end
end
uj5u.com熱心網友回復:
這個問題已經在這里回答了
但長話短說,你需要的是所謂的鋪墊,在鐵軌,你可以實作它與一個簡單和有效的寶石稱為卡米納里。
uj5u.com熱心網友回復:
您可以使用卡米納里或帕吉納特等幾顆寶石來實作此功能。對于此功能,無需進行自定義作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360861.html
