small medium large xlarge

Generic-user-small
12 Sep 2011, 13:03
Neil Chambers (1 post)

Chapter 12.3 is missing an explanation for the plumbing required in the orders_controller to get pagination to work. It doesn’t reference the script used to populate the database with dummy orders either. The bundled code in depot_q does have the necessary items though.

Generic-user-small
14 Sep 2011, 01:03
Ernesto Fries Urioste (1 post)

“script/load_orders.rb”

Order.transaction do (1..100).each do |i| Order.create(:name => “Customer #{i}”, :address => “#{i} Main Street”, :email => “customer-#{i}@example.com”, :pay_type => “Check”) end end

Also change the Orders Controller:

@orders = Order.paginate :page => params[:page], :order => 'created_at desc', :per_page => 10
Generic-user-small
02 Feb 2012, 18:59
djfox (9 posts)

Ernesto Where I have to put that in the controller “orders”?

This is the Error :

NoMethodError in OrdersController#index

undefined method `paginate’ for # Rails.root: C:/Documents and Settings/Administrador/Escritorio/Trabajo Final Ruby/work/depot/depot

Application Trace | Framework Trace | Full Trace app/controllers/orders_controller.rb:7:in `index’

Order_controller:

class OrdersController < ApplicationController
  # GET /orders
  # GET /orders.json
  
  def index
   #@orders = Order.all
 It´s ok here?-->  @orders = Order.paginate :page => params[:page], :order => 'created_at desc', :per_page => 10
    
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @orders }
    end
  end

  # GET /orders/1
  # GET /orders/1.json
  def show
    @order = Order.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @order }
    end
  end

  # GET /orders/new
  # GET /orders/new.json
  def new
    @cart = current_cart
    if @cart.line_items.empty?
      redirect_to store_url, notice: "Your cart is empty"
      return
    end
    
    @order = Order.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @order }
    end
  end

  # GET /orders/1/edit
  def edit
    @order = Order.find(params[:id])
  end

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(params[:order])
    @order.add_line_items_from_cart(current_cart)

    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        format.html { redirect_to store_url, notice: 'Thank you for your order.' }
        format.json { render json: @order, status: :created, location: @order }
      else
        @cart = current_cart
        format.html { render action: "new" }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /orders/1
  # PUT /orders/1.json
  def update
    @order = Order.find(params[:id])

    respond_to do |format|
      if @order.update_attributes(params[:order])
        format.html { redirect_to @order, notice: 'Order was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.json
  def destroy
    @order = Order.find(params[:id])
    @order.destroy

    respond_to do |format|
      format.html { redirect_to orders_url }
      format.json { head :ok }
    end
  end
end
Samr_small_pragsmall
02 Feb 2012, 18:52
Sam Ruby (634 posts)

Put it in the index method:

http://media.pragprog.com/titles/rails4/code/rails32/depot_q/app/controllers/orders_controller.rb

Generic-user-small
02 Feb 2012, 19:22
djfox (9 posts)

But says the method “paginate” is not defined

and in the page 235 PDF in the figure 26 “Showing ten orders out of more than a hundred​” I have no links in my app “Orders” “Products” and “Users”, let alone the button “Logout”. It will be an image of the same app but later chapters?

Generic-user-small
02 Feb 2012, 23:34
djfox (9 posts)

Solved problem !!… I put in the file GemFile gem ‘will_paginate’ and cmd (console windows) wrote ‘bundle install’ and voila! .. problem solved ..

I hope it helps someone ..!

Generic-user-small
03 Feb 2012, 20:57
farout (5 posts)

Don’t forget to restart the rails server in order to pick up the newly installed gem: rails s

You must be logged in to comment