2014-01-22 49 views
1

嗨,大家好我是Ruby on Rails的新手,當我嘗試添加新頁面時出現錯誤。當我點擊創建這是什麼出現頁面中的NoMethodError#create

當你沒有想到它時,你有一個零對象!您可能需要 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 

這是假定誤差根據所述框架 提取的源被happenign的_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 render' activesupport(3.0.9)lib/active_support/notifications.rb:54: instrument' actionpack (3.0.9) lib/action_view/template.rb:127:in 渲染 'ActionPack的(3.0.9)LIB/ACTION_VIEW /渲染/ partials.rb:333:在 render_partial' actionpack (3.0.9) lib/action_view/render/partials.rb:262:in渲染' 的ActiveSupport (3.0.9)LIB/active_support/notifications.rb:52:在instrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in儀器」 的ActiveSupport (3.0.9)lib/active_support/notifications.rb:52:in instrument' actionpack (3.0.9) lib/action_view/render/partials.rb:260:in render'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:in render'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' actionpack(3.0.9)lib/action_view/40:在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:幫手/ form_helper.rb:320:在0123中(3.0.9)lib/action_view/template.rb:127: render' actionpack (3.0.9) lib/action_view/render/rendering.rb:59:in _render_template'activesupport(3.0.9)lib/action_view/template.rb:135:render' activesupport (3.0.9) lib/active_support/notifications.rb:54:in ) 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/render.rb:26:render' actionpack (3.0.9) lib/abstract_controller/rendering.rb:115:in _render_template'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'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_strin g'actionpack(3.0.9) lib/abstract_controller/rendering.rb:93:in render' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:17:in render'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:in ms'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:在 cleanup_view_runtime' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:39:in渲染 ' ActionPack的(3.0.9)LIB/action_controller /金屬/ implicit_render.rb:4: send_action' actionpack (3.0.9) lib/action_controller/metal/implicit_render.rb:4:in send_action' ActionPack的(3.0。9)LIB/abstract_controller/base.rb:150:在 process_action' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:11:in process_action」 ActionPack的(3.0.9)LIB/abstract_controller/callbacks.rb:18:在 process_action' activesupport (3.0.9) lib/active_support/callbacks.rb:446:in 運行 _367793087__process_action_ _callbacks'的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'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'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' activesupport(3.0.9)lib/active_support/notifications.rb:52:in instrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in儀器 ' 的ActiveSupport(3.0.9)LIB/active_support/notifications.rb:52:在 instrument' actionpack (3.0.9) lib/action_controller/metal/instrumentation.rb:29:in process_action' ActionPack的(3.0.9)LIB/action_controller /金屬/ rescue.rb:17:在 process_action' actionpack (3.0.9) lib/abstract_controller/base.rb:119:in過程ActionPack的(3.0.9) lib/abstract_controller/rendering.rb:41:in process' actionpack (3.0.9) lib/action_controller/metal.rb:138:in dispatch'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:call' actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in dispatch' 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 call' 機架安裝(0.6.14)lib/rack/mount/code_generation.rb:93:在 recognize' rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in optimized_each'機架安裝 (0.6.14)lib/rack/mount/code_generation.rb:92:在recognize' rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in調用' ActionPack的(3.0.9)LIB/action_dispatch /路由/ route_set.rb:493:在 call' actionpack (3.0.9) lib/action_dispatch/middleware/best_standards_support.rb:17:in呼叫' ActionPack的(3.0.9)LIB/action_dispatch /中間件/ head.rb:14:call' rack (1.2.8) lib/rack/methodoverride.rb:24:in呼叫」 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 調用'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 調用'activerecord(3.0。 9)lib/active_record/query_cache.rb:32:在 call' activerecord (3.0.9) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in cache'active記錄(3.0.9)LIB/active_record/query_cache.rb:12: cache' activerecord (3.0.9) lib/active_record/query_cache.rb:31:in 呼叫的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呼叫'的ActiveSupport ( (1.2.8)lib/rack/sendfile.rb:106:在call' actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:in中調用' actionpack(3.0.9) lib/action_dispatch/(1.2.8)lib/rack/runtime.rb:17:在call' activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:in中調用'rack (1.2.8)lib/rack/lock.rb:在call' rack (1.2.8) lib/rack/lock.rb:13:in同步'rack(1.2.8) lib/rack/lock.rb:13:在(3.0.9) lib/rails/application.rb:168:在call' railties (3.0.9) lib/rails/application.rb:77:in發送'railties ) LIB /齒條/ content_length.rb:13:在call' rack (1.2.8) lib/rack/handler/webrick.rb:52:in服務 ' 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 運行' 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 開始' 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 開始' 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:在run' rack (1.2.8) lib/rack/server.rb:217:in開始'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 /軌道:6:在 `要求'腳本/導軌:6

任何形式的幫助讚賞。

+0

建議:輸入「rails g scaffold page title:string content:text」並將您的代碼與腳手架的代碼進行比較。它會幫助你理解軌道。 – emrahbasman

回答

0

確保你總是仔細閱讀錯誤,他們是理解錯誤的關鍵。在這裏你的錯誤說:

你有沒有對象,當你沒有想到它!您可能預期了Array的一個實例。評估nil收集時發生錯誤

這告訴我們在你的代碼的某個地方,你在一個零對象上調用collect。由於您沒有明確地在任何地方撥打nil.collect,因此假設您可能呼叫@some_instance_variable_that_is_not_set.collect是安全的。從那裏你應該看看它正在抱怨的行號(在本例中是第6行),並找到你要調用collect的變量,那可能是零。在這裏,你會發現@subjects.collect這是可能的罪魁禍首。將puts @subjects添加到您的控制器並檢查日誌以查看輸出結果。如果它爲零,則不會在控制器的create方法中設置該值。

這是您在瀏覽錯誤消息時應始終採用的方法。

+0

喬希感謝您的快速回復。令我感到困惑的是,當我要創建一個新頁面時,我正在使用收集來獲取該主題的下拉列表,並且據我所知,該行正在完成其工作,因爲我添加的每個主題都正在收集並我可以在創建新頁面時選擇它們,但是當我點擊「創建」時,它會拋出錯誤,就像是在工作,但它不能在同一時間工作。 – user2712521

相關問題