No Method Error - Rails 3 -
currently working on project, , have ran dead end following error:
you have nil object when didn't expect it! might have expected instance of array. error occurred while evaluating nil.each
i did research , found possibly because of pluralization. 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 |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 following line <% @schedules.each |schedule| %
<% @schedule.each |schedule| %`. when change made no method defined method explains have not defined variable each. rather confusing, trying many different ways , unable around this.
schedules controller
class schedulescontroller < applicationcontroller before_filter :authenticate, :only => [:index, :show, :new, :edit, :create, :update, :destroy] # /schedules # /schedules.xml def index @title = "schedules" @schedules = schedule.all respond_to |format| format.html # index.html.erb format.xml { render :xml => @schedules } end end # /schedules/1 # /schedules/1.xml def show @title = "show schedule" @schedule = schedule.find(params[:id]) respond_to |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 |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 |format| format.html #allowed.html.erb format.xml { render :xml => @schedule } end end # /schedules/new # /schedules/new.xml def new @title = "new schedule" @schedule = schedule.new respond_to |format| format.html # new.html.erb format.xml { render :xml => @schedule } end end # /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 |format| if @schedule.save format.html { redirect_to :action => "expected", :notice => 'schedule 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 |format| if @schedule.update_attributes(params[:schedule]) format.html { redirect_to(@schedule, :notice => 'schedule 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 |format| format.html { redirect_to(schedules_url) } format.xml { head :ok } end end # gets status given weekday out of 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 |schedule| # current weekday base_datetime = datetime.now curday = base_datetime.wday # check schedule each weekday (0..6).each |wday| if status_for_wday( schedule, wday ) == "e" offsetdays = wday - curday offsetdays += 7 if offsetdays <= 0 # next delivery , collection datetime 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 , find 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 order unless 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
i see nothing wrong here, , think should work - setting @schedules collection in controller? - combination of error messages suggests didn't - maybe try putting print statement in view see @schedules ...
Comments
Post a Comment