2014-01-30 4 views
0

몇 시간의 시도가 실패하면 여기에 내 질문을 게시합니다. 다음과 같이 나는 페이지 컨트롤러가 :@pages 인스턴스 변수가 nil 인 경우 : NillClass - 레일에 간단한 CMS 빌드

class PagesController < ApplicationController 

layout "admin" 

    # before_action :confirm_logged_in 
    # before_action :find_subject 

    def index 
    # @pages = Page.where(:subject_id => @subject.id).sorted 
    # @pages = @subject.pages.sorted 
    end 

    def show 
    @page = Page.find(params[:id]) 
    end 

    def new 
    @page = Page.new({:name => "Default"}) 
    @subjects = Subject.order('position ASC') 
    @page_count = Page.count + 1 
    end 

    def create 
    @page = Page.new(page_params) 
    if @page.save 
     flash[:notice] = "Page created successfully." 
     redirect_to(:action => 'index') 
    else 
     @subjects = Subject.order('position ASC') 
     @page_count = Page.count + 1 
     render('new') 
    end  
    end 

    def edit 
    @page = Page.find(params[:id]) 
    @subjects = Subject.order('position ASC') 
    @page_count = Page.count 
    end 

    def update 
    @page = Page.find(params[:id]) 
    if @page.update_attributes(page_params) 
     flash[:notice] = "Page updated successfully." 
     redirect_to(:action => 'show', :id => @page.id) 
    else 
     @subjects = Subject.order('position ASC') 
     @page_count = Page.count 
     render('edit') 
    end 
    end 

    def delete 
    @page = Page.find(params[:id]).destroy 
    flash[:notice] = "Page destroyed successfully." 
    redirect_to(:action => 'index') 
    end 


    private 

    def page_params 
     # same as using "params[:subject]", except that it: 
     # - raises an error if :subject is not present 
     # - allows listed attributes to be mass-assigned 
     params.require(:page).permit(:subject_id, :name, :permalink, :position, :visible) 
    end 

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

end 

를 다음과 같이 내 인덱스 뷰는 다음과 같습니다

<% @page_title = "Pages" %> 


<div class="pages index"> 

    <h2>Pages</h2> 

    <div class="pull-right"> 
     <%= link_to("Add New Page", {:action => 'new'}, :class => 'btn btn-success') %> 
    </div> 
    <br><br> 

    <table class="table table-striped table-bordered listing" summary="Page list"> 
     <tr class="header"> 
      <th>&nbsp;</th> 
      <th>Subject</th> 
      <th>Page</th> 
      <th>Permalink</th> 
      <th>Visible</th> 
      <th>Sections</th> 
      <th>Actions</th> 
     </tr> 

     <% @pages.each do |page| %> 

      <tr> 
       <td><%= page.position %></td> 
       <td><%= page.subject.name if page.subject %></td> 
       <td><%= page.name %></td> 
       <td><%= page.permalink %></td> 
       <td class="center"><%= status_tag(page.visible) %></td> 
       <td class="center"><%= page.sections.size %></td> 
       <td class="actions"> 
        <%= link_to("Show", {:action => 'show', :id => page.id, :subject_id => @subject.id}, :class => 'btn btn-primary btn-xs') %> 
        <%= link_to("Edit", {:action => 'edit', :id => page.id, :subject_id => @subject.id},  :class => 'btn btn-warning btn-xs') %> 
        <%= link_to("Delete", {:action => 'delete', :id => page.id, :subject_id => @subject.id}, :class => 'btn btn-danger btn-xs') %> 
       </td> 
      </tr> 

     <% end %> 
    </table> 
</div> 

내가 전무에 대한 정의되지 않은 메서드 '각'을 얻고있다 : NilClass을 나는 페이지 인덱스를 방문 할 때. 응용 프로그램에는 페이지 컨트롤러와 눈에 띄지 않는 차이가 없어도 잘 작동하는 다른 컨트롤러가 있습니다.

모든 포인터가 도움이 될 것입니다. 색인 라인의

답변

3

의 주석 하나

def index 
    @pages = Page.where(:subject_id => @subject.id).sorted 
    # @pages = @subject.pages.sorted 
end 

또는

def index 
    # @pages = Page.where(:subject_id => @subject.id).sorted 
    @pages = @subject.pages.sorted 
end 

UPDATE

내가 앱에 대해 잘 모르지만 당신이 필요할 것 그것을 사용하기 전에 @subject을 정의하십시오. 그리고 before_action :find_subject이 주석 처리되었으므로 에서 주석을 시도 울드 :

# before_action :find_subject 

before_action :find_subject 

그것은보기에 인스턴스를 정의하는 나쁜 습관이다. 이 <% @page_title = "Pages" %>은과 같이 컨트롤러 지수에 속하는 :

def index 
    @page_title = "Pages" 
    ... 
end 

가 그럼 그냥보기에 <%= @page_title %>를 호출합니다. PARAMS와

+0

해당 줄의 주석 처리를 제거했지만 성공하지는 못했습니다. 인덱스 페이지의 @pages 인스턴스에 문제가있는 것 같습니다. –

+0

글쎄, 여러분은'index' 메소드에서'@ subject'를 정의하지 않으므로 문제의 일부가 될 수도 있습니다. 내 대답을 예제로 업데이트하겠습니다. –

+0

귀하의 의견과 답변에 감사드립니다. 이 문제는 여전히 존재합니다. 해결 될 때 돌아와서 여기에 글을 쓸 것입니다. –

2

먼저 확인하는 일을 당신이 점점

    난 당신이 @subject 알아서해야한다고 생각
  1. :find_subject 않은 코멘트 before_filter @page
  2. 이 방법 컨트롤러의 정확한 @subject
  3. @pages = Page.where(:subject_id => @subject.id).sorted

plz이 모든 조건을 확인하십시오