1
votes

Hi guys I'm new to ruby on rails and I'm have an error when I try to add a new page. When I click create this is what appears

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

 class PagesController < ApplicationController

    layout 'admin'#apply the admin.html layout

    before_filter :confirm_logged_in #confirms if the user is logged in before entering the   page
    before_filter :find_subject

    def index
      list
      render('list')#runs pages/list.html
    end

    def list
    @pages = Page.where(:subject_id => @subject.id)#for the list view it finds the products   sorted,all of them
    end

    def show
     @page = Page.find(params[:id])#it finds the information of the product by its id and shows it in the show.html
    end

    def new
    @page = Page.new(:subject_id => @subject.id)#it creates a new page(product) in the corresponding subject(category) 
    @page_count = @subject.pages.size + 1 #the quantity of total (pages)products all of them
    @subjects = Subject.order('position ASC')# shows the subjects ordered ascending
      end

      def create
         #new_position = params[:page].delete(:position)

        @page = Page.new(params[:page]) #instantiate a new object using form parameters
      puts "Im here"
      if @page.save# save the object
      #@page.move_to_position(new_position)

        flash[:notice] = "Product created."
      redirect_to(:action => 'list', :subject_id => @page.subject_id)# if save succeeds, redirect to the list action
    else     
      @page_count = @subject.pages.size + 1 # products quantity is updated
      @subjects = Subject.order('position ASC')
      render('new') # if save fails, redisplay the form so user can fix problems
    end
  end

  def edit
    @page = Page.find(params[:id])# it find the page to be edited by its id
    @page_count = @subject.pages.size 
    @subjects = Subject.order('position ASC')
  end

  def update
    new_position = params[:page].delete(:position)

    @page = Page.find(params[:id])# find object using parameters

    if @page.update_attributes(params[:page])# update the object
      @page.move_to_position(new_position)

      flash[:notice] = "Page updated."
      redirect_to(:action => 'show', :id => @page.id, :subject_id => @page.subject_id)# if update succeeds, redirect to the list action
    else

      @page_count = @subject.pages.size
      @subjects = Subject.order('position ASC')
      render('edit')# if save fails, redisplay the form so user can fix problems
    end
  end

  def delete
    @page = Page.find(params[:id])#it finds the page by the id to be deleted
  end

  def destroy
    page = Page.find(params[:id])#it finds the page by the id to be destroyed
    page.move_to_position(nil)
    page.destroy
    flash[:notice] = "Product destroyed."
    redirect_to(:action => 'list', :subject_id => @subject.id)# redirect the user to the pages/list.html according to the corresponding subject(category)
  end

  private
   def find_subject
    if params[:subject_id]
      @subject = Subject.find_by_id(params[:subject_id])
    end
  end


end

I can add subjects with no problem at all and I can add admin users without issues the problem is with pages.

class SubjectsController < ApplicationController

  layout 'admin'#apply the admin.html layout

  before_filter :confirm_logged_in #confirms if the user is logged in before entering the page

  def index
    list
    render('list')#runs subjects/list.html all list.html are different
  end

  def list
    @subjects = Subject.order("subjects.position ASC")#for the list view it finds the categories sorted,all of them
  end

  def show
    @subject = Subject.find(params[:id])
  end

  def new
    @subject = Subject.new # creates a new subject(category)
    @subject_count = Subject.count + 1
  end

  def create
    new_position = params[:subject].delete(:position)

    @subject = Subject.new(params[:subject]) # instantiate a new object using form parameters

    if @subject.save #save the object

      @subject.move_to_position(new_position)

      flash[:notice] = "Category created."
      redirect_to(:action => 'list')# if save succeeds, redirect to list.html
    else

      @subject_count = Subject.count + 1
      render('new')#If save fails, redisplay the form so user can fix problems
    end
    end


  def edit
    @subject = Subject.find(params[:id])# it finds the subject to be edited
    @subject_count = Subject.count
  end

  def update
    new_position = params[:subject].delete(:position)#if updated it delete the current position

    @subject = Subject.find(params[:id])# find object using form parameters

    if @subject.update_attributes(params[:subject])#update the object(subject)
      @subject.move_to_position(new_position)#new position assigned

      flash[:notice] = "Subject updated."
      redirect_to(:action => 'show', :id => @subject.id)# if update succeeds, redirect to the list action
    else

      @subject_count = Subject.count
      render('edit')# if save fails, redisplay the form so user can fix problems
    end
  end

  def delete
    @subject = Subject.find(params[:id])#it finds thesubjectby the id to be deleted
  end

  def destroy
    subject = Subject.find(params[:id])#it finds the Subject(category)by the id to be destroyed
    subject.move_to_position(nil)
    subject.destroy
    flash[:notice] = "Subject destroyed."
    redirect_to(:action => 'list')# redirected to the list.html 
  end

end

This is the _form.html.erb where the error is supposed to be happenign according to the framework Extracted source (around line #6):

3: <table summary="Products form fields">
4:   <tr>
5:     <th><%= f.label(:subject_id, "Category") %></th>
6:  <td><%= f.select(:subject_id, @subjects.collect {|s| [s.name, s.id]}) %></td>  </tr>

<%= error_messages_for(@pages) %>

<table summary="Products form fields">
  <tr>
    <th><%= f.label(:subject_id, "Category") %></th>
    <td><%= f.select(:subject_id, @subjects.collect {|s| [s.name, s.id]}) %></td>  </tr>
  <tr>
    <th><%= f.label(:product_name) %></th>
    <td><%= f.text_field(:product_name) %></td>
  </tr>
  <tr>
    <th><%= f.label(:description) %></th>
    <td><%= f.text_area(:description) %></td>
  </tr>
  <tr>
    <th><%= f.label(:min_description) %></th>
    <td><%= f.text_area(:min_description) %></td>
  </tr>
  <tr>
    <th><%= f.label(:brand) %></th>
    <td><%= f.text_field(:brand) %></td>
    <tr>
    <th><%= f.label(:quantity) %></th>
    <td><%= f.text_field(:quantity) %></td>
  </tr>
  <tr>
    <th><%= f.label(:price) %></th>
    <td><%= f.text_field(:price) %></td>
  </tr>
  <tr>
    <th><%= f.label(:size) %></th>
    <td><%= f.text_field(:size) %></td>
  </tr>
  <tr>
    <th><%= f.label(:head_size) %></th>
    <td><%= f.text_field(:head_size) %></td>
  </tr>
  <tr>
    <th><%= f.label(:racquets_weight) %></th>
    <td><%= f.text_field(:racquets_weight) %></td>
  </tr>
  <tr>
    <th><%= f.label(:category) %></th>
    <td><%= f.text_field(:category) %></td>
  </tr>
  <tr>
    <th><%= f.label(:image_url) %></th>
    <td><%= f.text_field(:image_url) %></td>

  </tr>
</table>

Framework trace

actionpack (3.0.9) lib/action_view/template.rb:135:in send' actionpack (3.0.9) lib/action_view/template.rb:135:inrender' activesupport (3.0.9) lib/active_support/notifications.rb:54:in instrument' actionpack (3.0.9) lib/action_view/template.rb:127:in render' actionpack (3.0.9) lib/action_view/render/partials.rb:333:in render_partial' actionpack (3.0.9) lib/action_view/render/partials.rb:262:inrender' activesupport (3.0.9) lib/active_support/notifications.rb:52:in instrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:ininstrument' activesupport (3.0.9) lib/active_support/notifications.rb:52:in instrument' actionpack (3.0.9) lib/action_view/render/partials.rb:260:inrender' actionpack (3.0.9) lib/action_view/render/partials.rb:378:in _render_partial' actionpack (3.0.9) lib/action_view/render/rendering.rb:22:inrender' actionpack (3.0.9) lib/action_view/helpers/capture_helper.rb:40:in capture' actionpack (3.0.9) lib/action_view/helpers/capture_helper.rb:172:in with_output_buffer' actionpack (3.0.9) lib/action_view/helpers/capture_helper.rb:40:in capture' actionpack (3.0.9) lib/action_view/helpers/form_helper.rb:545:infields_for' actionpack (3.0.9) lib/action_view/helpers/form_helper.rb:320:in form_for' actionpack (3.0.9) lib/action_view/template.rb:135:in send' actionpack (3.0.9) lib/action_view/template.rb:135:in render' activesupport (3.0.9) lib/active_support/notifications.rb:54:in instrument' actionpack (3.0.9) lib/action_view/template.rb:127:in render' actionpack (3.0.9) lib/action_view/render/rendering.rb:59:in _render_template' activesupport (3.0.9) lib/active_support/notifications.rb:52:in instrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in instrument' activesupport (3.0.9) lib/active_support/notifications.rb:52:in instrument' actionpack (3.0.9) lib/action_view/render/rendering.rb:56:in_render_template' actionpack (3.0.9) lib/action_view/render/rendering.rb:26:in render' actionpack (3.0.9) lib/abstract_controller/rendering.rb:115:in _render_template' actionpack (3.0.9) lib/abstract_controller/rendering.rb:109:in render_to_body' actionpack (3.0.9) lib/action_controller/metal/renderers.rb:47:in render_to_body' actionpack (3.0.9) lib/action_controller/metal/compatibility.rb:55:in render_to_body' actionpack (3.0.9) lib/abstract_controller/rendering.rb:102:in render_to_string' actionpack (3.0.9) lib/abstract_controller/rendering.rb:93:in render' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:17:inrender' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:40:in render' activesupport (3.0.9) lib/active_support/core_ext/benchmark.rb:5:in ms' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/benchmark.rb:308:in realtime' activesupport (3.0.9) lib/active_support/core_ext/benchmark.rb:5:inms' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:40:in render' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:78:in cleanup_view_runtime' activerecord (3.0.9) lib/active_record/railties/controller_runtime.rb:15:in cleanup_view_runtime' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:39:inrender' actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:in send_action' actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:insend_action' actionpack (3.0.9) lib/abstract_controller/base.rb:150:in process_action' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:11:inprocess_action' actionpack (3.0.9) lib/abstract_controller/callbacks.rb:18:in process_action' activesupport (3.0.9) lib/active_support/callbacks.rb:446:in run_367793087__process_action_524098549_callbacks' activesupport (3.0.9) lib/active_support/callbacks.rb:410:in send' activesupport (3.0.9) lib/active_support/callbacks.rb:410:in _run_process_action_callbacks' activesupport (3.0.9) lib/active_support/callbacks.rb:94:in send' activesupport (3.0.9) lib/active_support/callbacks.rb:94:inrun_callbacks' actionpack (3.0.9) lib/abstract_controller/callbacks.rb:17:in process_action' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:30:inprocess_action' activesupport (3.0.9) lib/active_support/notifications.rb:52:in instrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:ininstrument' activesupport (3.0.9) lib/active_support/notifications.rb:52:in instrument' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:29:inprocess_action' actionpack (3.0.9) lib/action_controller/metal/rescue.rb:17:in process_action' actionpack (3.0.9) lib/abstract_controller/base.rb:119:inprocess' actionpack (3.0.9) lib/abstract_controller/rendering.rb:41:in process' actionpack (3.0.9) lib/action_controller/metal.rb:138:indispatch' actionpack (3.0.9) lib/action_controller/metal/rack_delegation.rb:14:in dispatch' actionpack (3.0.9) lib/action_controller/metal.rb:178:in action' actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in call' actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:indispatch' actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:27:in call' rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:incall' rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in recognize' rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:inoptimized_each' rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in recognize' rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:incall' actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:493:in call' actionpack (3.0.9) lib/action_dispatch/middleware/best_standards_support.rb:17:incall' actionpack (3.0.9) lib/action_dispatch/middleware/head.rb:14:in call' rack (1.2.8) lib/rack/methodoverride.rb:24:incall' actionpack (3.0.9) lib/action_dispatch/middleware/params_parser.rb:21:in call' actionpack (3.0.9) lib/action_dispatch/middleware/flash.rb:182:in call' actionpack (3.0.9) lib/action_dispatch/middleware/session/abstract_store.rb:149:in call' actionpack (3.0.9) lib/action_dispatch/middleware/cookies.rb:302:in call' activerecord (3.0.9) lib/active_record/query_cache.rb:32:in call' activerecord (3.0.9) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in cache' activerecord (3.0.9) lib/active_record/query_cache.rb:12:in cache' activerecord (3.0.9) lib/active_record/query_cache.rb:31:in call' activerecord (3.0.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in call' actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:46:incall' activesupport (3.0.9) lib/active_support/callbacks.rb:416:in _run_call_callbacks' actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:44:in call' rack (1.2.8) lib/rack/sendfile.rb:106:in call' actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:incall' actionpack (3.0.9) lib/action_dispatch/middleware/show_exceptions.rb:47:in call' railties (3.0.9) lib/rails/rack/logger.rb:13:incall' rack (1.2.8) lib/rack/runtime.rb:17:in call' activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:incall' rack (1.2.8) lib/rack/lock.rb:13:in call' rack (1.2.8) lib/rack/lock.rb:13:insynchronize' rack (1.2.8) lib/rack/lock.rb:13:in call' actionpack (3.0.9) lib/action_dispatch/middleware/static.rb:30:incall' railties (3.0.9) lib/rails/application.rb:168:in call' railties (3.0.9) lib/rails/application.rb:77:insend' railties (3.0.9) lib/rails/application.rb:77:in method_missing' railties (3.0.9) lib/rails/rack/log_tailer.rb:14:incall' rack (1.2.8) lib/rack/content_length.rb:13:in call' rack (1.2.8) lib/rack/handler/webrick.rb:52:inservice' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:104:in service' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:65:in run' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:173:in start_thread' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:162:in start' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:162:in start_thread' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:95:in start' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:92:in each' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:92:in start' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:23:in start' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:82:in start' rack (1.2.8) lib/rack/handler/webrick.rb:13:in run' rack (1.2.8) lib/rack/server.rb:217:instart' railties (3.0.9) lib/rails/commands/server.rb:65:in start' railties (3.0.9) lib/rails/commands.rb:30 railties (3.0.9) lib/rails/commands.rb:27:in tap' railties (3.0.9) lib/rails/commands.rb:27 script/rails:6:in `require' script/rails:6

Any kind of help appreciated.

1
Advice: type "rails g scaffold page title:string content:text" and compare your codes with the scaffolded one. It will help you to understand rails.emrahbasman

1 Answers

0
votes

Make sure you always read the errors carefully, they are the key to understanding what's going wrong. Here your error says:

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

This tells us that somewhere in your code you are calling collect on a nil object. Since you aren't explicitly calling nil.collect anywhere it is safe to assume you are probably calling @some_instance_variable_that_is_not_set.collect. From there you should look at the line number that it is complaining about(in this case line 6) and find any variables you are calling collect on that could be nil. Here, you will find @subjects.collect which is the likely culprit. Add a puts @subjects to your controller and check the logs to see what the output is. If it's nil, you are not setting the value in your controller's create method.

This is the methodology you should always take when going through error messages.