Home > Software engineering >  how to make a limited output of items on the page on rails?
how to make a limited output of items on the page on rails?

Time:11-20

I need to make sure that no more than 24 products are displayed on the page at the same time, if there are more than 24 of them, then there should be a button that will redirect me to the second page with products.
It is also necessary that when going to the next page it is somehow reflected in the link of the site, ie on the first page the link should be / products? PageIndex = 1, and on the second / products? PageIndex = 2
this is my index.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 %>

And this is my controller

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

CodePudding user response:

This question has already been answered here

But long story short, what you need is called pagination and in Rails you can achieve it with a simple and effective gem called Kaminari.

CodePudding user response:

You can achieve this functionality by using several gems available out there like Kaminari or Paginate. There is no point to do custom work for this functionality.

  • Related