Deej
Deej

Reputation: 5352

No Method Error - Rails 3

Currently working on a project, and have ran into a dead end with the following error:

You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each

I did some research and found that it could possibly be because of pluralization. The code follows:

   <h1>Listing Expectedd Schedules</h1>

<table>
    <tr>
        <th>From</th>
        <th>To</th>
        <th>State</th>
        <th>Delivery Time</th>
        <th>Collection Time</th></>
        <th>Edit</th>
    </tr>

    <% @schedules.each do |schedule| %>

    <tr>
        <td><%= schedule.from_location_id %></td>
        <td><%= schedule.to_location_id %></td>
        <td><%= schedule.schedule_type %></td>
        <td><%= schedule.delivery_time %></td>
        <td><%= schedule.collection_time%></td>
    </tr>
    <% end %>
</table>

I changed the following line <% @schedules.each do |schedule| % to <% @schedule.each do |schedule| %`. However when that change is made I get an No method defined method that explains that I have not defined the variable each. This is rather confusing, trying many different ways and unable to get around this.

Schedules Controller

class SchedulesController < ApplicationController
  before_filter :authenticate, :only => [:index, :show, :new, :edit, :create, :update, :destroy]
  # GET /schedules
  # GET /schedules.xml
  def index
    @title = "Schedules"
    @schedules = Schedule.all

     respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @schedules }
    end
  end

  # GET /schedules/1
  # GET /schedules/1.xml
  def show
    @title = "Show schedule"
    @schedule = Schedule.find(params[:id])


    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @schedule }
    end
  end

  def allowed
    @title = "Allowed schedules"
    @schedule = Schedule.new
    @clients = Client.find(:all)

    respond_to do |format|
      format.html #allowed.html.erb
      format.xml { render :xml => @schedule }

    end
  end

  def expected
    @title = "Expected schedule"
    #@schedule = Schedule.find(params[:all])
    @schedule = Schedule.new

    respond_to do |format|
      format.html #allowed.html.erb
      format.xml { render :xml => @schedule }
    end
  end

  # GET /schedules/new
  # GET /schedules/new.xml
  def new
    @title = "New schedule"
    @schedule = Schedule.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @schedule }
    end
  end

  # GET /schedules/1/edit
  def edit
    @title = "Edit schedule"
    @schedule = Schedule.find(params[:id])
  end

  def complete
    @title = "Complete schedule"
    @schedule = Schedule.new
  end
  # POST /schedules
  # POST /schedules.xml
  def create
    @schedule = Schedule.new(params[:schedule])

    respond_to do |format|
      if @schedule.save
        format.html { redirect_to :action =>  "expected", :notice => 'Schedule was successfully created.' }
        format.xml  { render :xml => @schedule, :status => :created, :location => @schedule }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @schedule.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /schedules/1
  # PUT /schedules/1.xml
  def update
    @schedule = Schedule.find(params[:id])

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

  # DELETE /schedules/1
  # DELETE /schedules/1.xml
  def destroy
    @schedule = Schedule.find(params[:id])
    @schedule.destroy

    respond_to do |format|
      format.html { redirect_to(schedules_url) }
      format.xml  { head :ok }
    end
  end

  # Gets the status for the given weekday out of the schedule record
  def self.status_for_wday( schedule, wday )
    case wday
    when 0
      schedule.sun_status
    when 1
      schedule.mon_status
    when 2
      schedule.tue_status
    when 3
      schedule.wed_status
    when 4
      schedule.thu_status
    when 5
      schedule.fri_status
    when 6
      schedule.sat_status
    else
    ""
    end
  end

  def self.create_orders
    schedules = Schedule.all

    schedules.each do |schedule|

    # Get current weekday
      base_datetime = DateTime.now
      curday = base_datetime.wday

      # check the schedule for each weekday
      (0..6).each do |wday|
        if status_for_wday( schedule, wday ) == "E"
          offsetdays = wday - curday
          offsetdays += 7 if offsetdays <= 0

          # Get the next delivery and collection datetime for the given schedule
          offset_datetime = base_datetime + offsetdays
          del_datetime = DateTime.parse( offset_datetime.strftime( "%Y-%m-%d " ) + schedule.delivery_time )
          col_datetime = DateTime.parse( offset_datetime.strftime( "%Y-%m-%d " ) + schedule.collection_time )

          # Try and find a matching order
          order = Order.where( :client_id => schedule.client_id,
          :order_type => schedule.schedule_type,
          :from_location_id => schedule.from_location_id,
          :to_location_id => schedule.to_location_id,
          'delivery_datetime' => del_datetime.strftime( "%Y-%m-%d %H:%M:%S.%6N" ),
          'collection_datetime' => col_datetime.strftime( "%Y-%m-%d %H:%M:%S.%6N" ) )

          # Create the order unless it already exists
          unless order.any?
            order = Order.create!( :status => "Estimated",
            :client_id => schedule.client_id,
            :order_type => schedule.schedule_type,
            :from_location_id => schedule.from_location_id,
            :to_location_id => schedule.to_location_id,
            :estimated_pallets => schedule.estimated_pallets,
            :delivery_datetime => del_datetime,
            :collection_datetime => col_datetime )
          end
        end
      end
    end
  end

  private

  def authenticate
    deny_access unless signed_in?
  end
end

Upvotes: 0

Views: 326

Answers (1)

chrispanda
chrispanda

Reputation: 3224

I see nothing wrong here, and think it should work - so are you setting the @schedules collection in the controller? - the combination of error messages suggests you probably didn't - maybe try putting a print statement in the view to see what @schedules actually is ...

Upvotes: 1

Related Questions