没有方法错误 - Rails 3

时间:2011-07-01 09:06:44

标签: ruby-on-rails-3

目前正在处理一个项目,并遇到以下错误而陷入了死胡同:

当你没想到它时,你有一个零对象! 您可能期望一个Array实例。 评估nil.each时发生错误

我做了一些研究,发现它可能是因为多元化。代码如下:

   <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>

我将以下行<% @schedules.each do |schedule| %更改为&lt;%@ schedule.each do | schedule | %`。但是,当进行该更改时,我得到一个No方法定义的方法,该方法解释了我没有定义每个变量。这是相当混乱的,尝试了许多不同的方式,无法解决这个问题。

安排控制器

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

1 个答案:

答案 0 :(得分:1)

我认为这里没有错,认为它应该有效 - 所以你在控制器中设置@schedules集合了吗? - 错误消息的组合表明你可能没有 - 也许尝试在视图中放置一个print语句来查看@schedules实际上是什么......