2014-01-30 47 views
0

經過幾個小時的失敗嘗試後,我在這裏發佈我的問題。我有一個頁面控制器如下:@pages實例變量爲零:NillClass - 在Rails中構建一個簡單的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被註釋掉了,你sh烏爾德試着從在取消它:

# before_action :find_subject 

before_action :find_subject 

ALSO

這是不好的做法在視圖來定義實例。這<% @page_title = "Pages" %>所屬的控制指標,像這樣:

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

然後,只需調用<%= @page_title %>在您的視圖。

+0

我嘗試過取消該行的註釋,但沒有成功。看起來問題出在索引頁上的@pages實例。 –

+0

那麼,你沒有在'index'方法中定義'@ subject',這也是問題的一部分。我會用一個例子更新我的答案。 –

+0

感謝您的反饋和答覆。問題仍然存在,當它解決後,我會回來寫在這裏。 –

2

首先檢查使用參數那你是什麼讓在控制器的確切@subject

  1. 我想你應該採取的@subject關懷和@page
  2. 的before_filter :find_subject未評論這種方法
  3. @pages = Page.where(:subject_id => @subject.id).sorted

查看全部條件