2014-01-22 10 views
1

안녕하세요 저는 레일즈에서 루비를 처음 사용하고 새 페이지를 추가하려고 할 때 오류가 있습니다. 내가 만드는 것을 클릭하면 나타나는 것은 무엇입니까NoMethodError in Pages #

당신이 그것을 기대하지 않았을 때 당신은 개체가 없습니다! 에 Array 인스턴스가있을 수 있습니다. 나는 전혀 문제없이 과목을 추가 할 수 있습니다 내가 문제가 페이지에 함께 문제없이 관리자 사용자를 추가 할 수 있습니다

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 

nil.collect 을 평가하는 동안 오류가 발생했습니다.

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> 

프레임 워크 추적

액션 팩 (3.0.9) lib/action_view/template.rb : 135 : send' actionpack (3.0.9) lib/action_view/template.rb:135:in에서 렌더링 ' 활성/비활성 알림/통지 : rb : 54 : instrument' actionpack (3.0.9) lib/action_view/template.rb:127:in 렌더링 52 : instrument' activesupport (3.0.9) lib/active_support/notifications/instrumenter.rb:21:in 악기 ' activesupport': 333 : lib 디렉토리/ACTION_VIEW// partials.rb 렌더링 actionpack (3.0.9)을 에 render_partial' actionpack (3.0.9) lib/action_view/render/partials.rb:262:in 렌더링 ' (3.0.9) lib 디렉토리/active_support/notifications.rb activesupport (3.0.9) lib/active_support/notifications.rb : 52 : instrument' actionpack (3.0.9) lib/action_view/render/partials.rb:260:in 렌더링 '액션 팩 (3.0.9) (3.0.9) (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 field_for ' lib/action_view/helpers/capture_helper.rb : 40 : capture' actionpack (3.0.9) lib/action_view/helpers/capture_helper.rb:172:in 에서 with_output_buffer'액션 팩 (3.0.9) helpers/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) 라이브러리에서'actionpack (3.0.9) lib/action_view/template.rb : 135 : render' activesupport (3.0.9) lib/active_support/notifications.rb:54:in 악기에서 'actionpack) 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 : instrument' actionpack (3.0.9) lib/action_view/render/rendering.rb:56:in _render_template' actionpack (3.0.9) lib/action_view/render/(3.0.9) render_to_body' actionpack (3.0.9) lib/action_controller/metal/renderers.rb:47:in 렌더 _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) (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'c :/RailsInstaller (3.0.9) lib/abstract_controller/rendering.rb : 93 : render' actionpack (3.0.9) lib/action_controller/metal/rendering.rb:17:in에서 ' /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 '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'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 : 실행 _367793087__process_action_ 524,098,549 _callbacks process_action' activesupport (3.0.9) lib/active_support/callbacks.rb:446:in 에서'activesupport (3.0 .09) 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 : send' activesupport (3.0.9) lib/active_support/callbacks.rb:94:in run_callbacks'actionpack (3.0.9) lib/abstract_controller/콜백 .rb : 17 : in 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 : 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 : process' actionpack (3.0.9) lib/action_controller/metal.rb:138:in 디스패치에서 'actionpack (3.0.9) LIB/action_controller/금속/rack_delegation.rb : 14 : dispatch' actionpack (3.0.9) lib/action_controller/metal.rb:178:in 액션'actionpack (3.0.9) lib 디렉토리/action_dispatch/라우팅/route_set.rb : 62 :call' actionpack (3.0.9) lib/action_dispatch/routing/route_set.rb:62:in 파견 ': 27 call' rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in 호출 actionpack (3.0.9) lib 디렉토리/action_dispatch/라우팅/route_set.rb'에서랙 마운트 (0.6.14) lib/rack/mount/code_generation.rb : 93 : recognize' rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in 랙 마운트 (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 캐시 '활성 기록 (3.0.9) lib 디렉토리/active_record/query_cache.rb : 12 : cache' activerecord (3.0.9) lib/active_record/query_cache.rb:31:in 전화 '354 : 액티브 (3.0.9) lib 디렉토리/active_record/connection_adapters/초록/connection_pool.rb call' actionpack (3.0.9) lib/action_dispatch/middleware/callbacks.rb:46:in 호출'에서 activesupport ((3.0.9) lib/action_dispatch/sendfile.rb : 106 : call' actionpack (3.0.9) lib/action_dispatch/middleware/remote_ip.rb:48:in에서 액션 팩 (3.0.9)을 호출하십시오. (1.2.8) lib/rack/runtime.rb : 17 : call' activesupport (3.0.9) lib/active_support/cache/strategy/local_cache.rb:72:in에 전화하십시오. (1.2.8) lib/rack/lock.rb : 13 : 012 : call' rack (1.2.8) lib/rack/lock.rb:13:in에서 '랙 (1.2.8) lib/rack/lock.rb : 13 :에 동기화210 전화를 걸다 '02.전화 '랙 (1.2.9) 012 전화 번호부 (3.0.9) lib/rails/application.rb : 168 : call' railties (3.0.9) lib/rails/application.rb:77:in에'call' railties (3.0.9) lib/rails/application.rb:77:in lib/rails/application.rb :) 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) C service' c:/RailsInstaller/Ruby1.8.7/lib/ruby/1.8/webrick/httpserver.rb:65:in 실행에서'/ 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 시작 ' 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 시작'랙 (1.2.8) lib/rack/handler/(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 에서 탭 'railties (3.0.9) lib/rails/commands.rb : 27 스크립트/webrick.rb : 13 : run' rack (1.2.8) lib/rack/server.rb:217:in에서 시작합니다. 레일 : 6 : 에서`require '스크립트/레일 : 6

모든 종류의 도움을 주셨습니다.

+0

조언 : "rails g scaffold page title : string content : text"를 입력하고 코드를 스캐 폴딩 된 것과 비교하십시오. 그것은 레일을 이해하는 데 도움이 될 것입니다. – emrahbasman

답변

0

항상 오류를주의 깊게 읽어야합니다. 오류가 무엇인지 잘못 이해하는 것이 중요합니다. 여기에 귀하의 오류 :

당신이 그것을 기대하지 않았을 때 당신은 개체가 없습니다! 당신은 Array의 인스턴스를 예상했을 것입니다. nil.collect를 평가하는 중 오류가 발생했습니다.

이것은 코드의 어딘가에서 nil 객체에 collect을 호출하고 있음을 나타냅니다. 명시 적으로 nil.collect을 호출하지 않으므로 가정 할 때 안전하다고 생각하면 @some_instance_variable_that_is_not_set.collect이라고 생각할 수 있습니다. 거기에서 (이 경우 6 행에서) 불평하는 행 번호를보고 collect을 호출하는 변수를 찾으면 그 변수는 0이 될 수 있습니다. 여기에서 @subjects.collect을 발견 할 수 있습니다. puts @subjects을 컨트롤러에 추가하고 로그를 확인하여 출력 내용을 확인하십시오. 그것이 없다면 컨트롤러의 create 메소드에서 값을 설정하지 않습니다.

이것은 오류 메시지를 읽을 때 항상 수행해야하는 방법입니다.

+0

조쉬는 빠른 답장을 보내 주셔서 감사합니다. 저를 혼란스럽게하는 이유는 제가 새 페이지를 만들려고 할 때 주제를 모으기 위해 콜렉션을 사용하고 제가 추가하는 모든 주제가 모아지고 있기 때문에 선이 그 일을하고 있다고 말할 수있는 한은 나는 새로운 페이지를 만들 때 그 중 하나를 선택할 수 있지만 "생성"을 클릭하면 오류가 발생하고 작동하는 것처럼 보이지만 동시에 작동하지 않습니다. – user2712521