Pages#中的NoMethodError创建

时间:2014-01-22 22:17:38

标签: ruby-on-rails

嗨,大家好我是ruby on rails的新手,当我尝试添加新页面时,我遇到了错误。当我点击创建时,这就是出现的内容

  

当你没有期待它时,你有一个零对象!你可能有   期待一个Array的实例。评估时发生错误   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

我可以添加没有任何问题的主题,我可以添加管理员用户而不会出现问题与页面有关。

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

这是_form.html.erb,根据框架,错误应该是签名的 提取的来源(第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>

框架跟踪

  

actionpack(3.0.9)lib / action_view / template.rb:135:在send' actionpack (3.0.9) lib/action_view/template.rb:135:in渲染&#39;   activesupport(3.0.9)lib / active_support / notifications.rb:54:in   instrument' actionpack (3.0.9) lib/action_view/template.rb:127:in 渲染&#39; 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:in渲染&#39;的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&#39;   activesupport(3.0.9)lib / active_support / notifications.rb:52:in   instrument' actionpack (3.0.9) lib/action_view/render/partials.rb:260:in渲染&#39; actionpack(3.0.9)   lib / action_view / render / partials.rb:378:在_render_partial' actionpack (3.0.9) lib/action_view/render/rendering.rb:22:in渲染&#39; 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&#39; actionpack(3.0.9)   lib / action_view / helpers / capture_helper.rb:40:capture' actionpack (3.0.9) lib/action_view/helpers/form_helper.rb:545:in fields_for&#39;   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 发送&#39; actionpack(3.0.9)lib / action_view / template.rb:135:在render' activesupport (3.0.9) lib/active_support/notifications.rb:54:in 工具&#39; 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&#39; 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&#39; 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&#39;   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&#39; actionpack(3.0.9)   lib / abstract_controller / rendering.rb:109:在render_to_body' actionpack (3.0.9) lib/action_controller/metal/renderers.rb:47:in render_to_body&#39; actionpack(3.0.9)   lib / action_controller / metal / compatibility.rb:55:在render_to_body' actionpack (3.0.9) lib/abstract_controller/rendering.rb:102:in render_to_string&#39; actionpack(3.0.9)   lib / abstract_controller / rendering.rb:93:在render' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:17:in渲染&#39; ActionPack的   (3.0.9)lib / action_controller / metal / instrumentation.rb:40:render' activesupport (3.0.9) lib/active_support/core_ext/benchmark.rb:5:in ms&#39; C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/benchmark.rb:308:在   realtime' activesupport (3.0.9) lib/active_support/core_ext/benchmark.rb:5:in MS&#39; actionpack(3.0.9)   lib / action_controller / metal / instrumentation.rb:40:在render' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:78:in cleanup_view_runtime&#39; activerecord(3.0.9)   LIB / active_record / railties / controller_runtime.rb:15:在   cleanup_view_runtime' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:39:in渲染&#39;   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:in send_action&#39;   actionpack(3.0.9)lib / abstract_controller / base.rb:150:in   process_action' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:11:in process_action&#39;   actionpack(3.0.9)lib / abstract_controller / callbacks.rb:18:in   process_action' activesupport (3.0.9) lib/active_support/callbacks.rb:446:in 运行 _367793087__process_action_ 524098549 _callbacks&#39;的ActiveSupport   (3.0.9)lib / active_support / callbacks.rb:410:在send' activesupport (3.0.9) lib/active_support/callbacks.rb:410:in _ run_process_action_callbacks&#39; activesupport(3.0.9)   lib / active_support / callbacks.rb:94:in send' activesupport (3.0.9) lib/active_support/callbacks.rb:94:in run_callbacks&#39; ActionPack的   (3.0.9)lib / abstract_controller / callbacks.rb:17:在process_action' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:30:in process_action&#39;   activesupport(3.0.9)lib / active_support / notifications.rb:52:in   instrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in仪器&#39;   activesupport(3.0.9)lib / active_support / notifications.rb:52:in   instrument' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:29:in process_action&#39;   actionpack(3.0.9)lib / action_controller / metal / rescue.rb:17:in   process_action' actionpack (3.0.9) lib/abstract_controller/base.rb:119:in过程&#39; actionpack(3.0.9)   lib / abstract_controller / rendering.rb:41:在process' actionpack (3.0.9) lib/action_controller/metal.rb:138:in发送&#39; 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 动作&#39; actionpack(3.0.9)   lib / action_dispatch / routing / route_set.rb:62:在call' actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in发送&#39;   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:in呼叫&#39;   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:in optimized_each&#39;机架式   (0.6.14)lib / rack / mount / code_generation.rb:92:in recognize' rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in call&#39;   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:in呼叫&#39;   actionpack(3.0.9)lib / action_dispatch / middleware / head.rb:14:in call' rack (1.2.8) lib/rack/methodoverride.rb:24:in call&#39; ActionPack的   (3.0.9)lib / action_dispatch / middleware / params_parser.rb:21:call' actionpack (3.0.9) lib/action_dispatch/middleware/flash.rb:182:in 来电&#39; actionpack(3.0.9)   lib / action_dispatch / middleware / session / abstract_store.rb:149:call' actionpack (3.0.9) lib/action_dispatch/middleware/cookies.rb:302:in 来电&#39; 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 缓存&#39; 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 呼叫&#39; activerecord(3.0.9)   LIB / active_record / connection_adapters /抽象/ connection_pool.rb:354:在   call' actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:46:in呼叫&#39;的ActiveSupport   (3.0.9)lib / active_support / callbacks.rb:416:_run_call_callbacks' actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:44:in 来电&#39; rack(1.2.8)lib / rack / sendfile.rb:106:在call' actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:in电话&#39;   actionpack(3.0.9)   lib / action_dispatch / middleware / show_exceptions.rb:47:in call' railties (3.0.9) lib/rails/rack/logger.rb:13:in来电&#39;机架(1.2.8)   lib / rack / runtime.rb:17:in call' activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:in call&#39;架   (1.2.8)lib / rack / lock.rb:13:in call' rack (1.2.8) lib/rack/lock.rb:13:in同步&#39;机架(1.2.8)   lib / rack / lock.rb:13:in call' actionpack (3.0.9) lib/action_dispatch/middleware/static.rb:30:in来电&#39;铁路(3.0.9)   lib / rails / application.rb:168:在call' railties (3.0.9) lib/rails/application.rb:77:in发送&#39;铁路(3.0.9)   lib / rails / application.rb:77:在method_missing' railties (3.0.9) lib/rails/rack/log_tailer.rb:14:in电话&#39;机架(1.2.8)   lib / rack / content_length.rb:13:in call' rack (1.2.8) lib/rack/handler/webrick.rb:52:in service&#39;   C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:104:在   service' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:65:in 运行&#39;   C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:173:在   start_thread' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:162:in 开始&#39;   C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:162:在   start_thread' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:95:in 开始&#39;   C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:92:在   each' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:92:in 开始&#39;   C:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:23:在   start' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/server.rb:82:in 开始&#39; rack(1.2.8)lib / rack / handler / webrick.rb:13:in run' rack (1.2.8) lib/rack/server.rb:217:in start&#39;铁路(3.0.9)   lib / rails / commands / server.rb:65:在start' railties (3.0.9) lib/rails/commands.rb:30 railties (3.0.9) lib/rails/commands.rb:27:in 点击&#39; railties(3.0.9)lib / rails / commands.rb:27 script / rails:6:in   `需要&#39;脚本/轨道:6

任何形式的帮助。

1 个答案:

答案 0 :(得分:0)

确保您始终仔细阅读错误,这是了解错误的关键。这里你的错误说:

  

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

时发生错误

这告诉我们,在代码中的某个位置,您在nil对象上调用collect。由于您没有明确地在任何地方调用nil.collect,因此可以安全地假设您可能正在调用@some_instance_variable_that_is_not_set.collect。从那里你应该看看它抱怨的行号(在这种情况下第6行)并找到你正在调用的任何变量collect,这可能是零。在这里,你会发现@subjects.collect可能是罪魁祸首。将puts @subjects添加到控制器并检查日志以查看输出结果。如果它为零,则表示您未在控制器的create方法中设置该值。

这是您在浏览错误消息时应始终采用的方法。