本文介绍了@pages实例变量为nil:NillClass-在Rails中构建一个简单的CMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过数小时的失败尝试后,我不得不在这里发表我的问题.我有一个页面控制器,如下所示:

After several hours of failed attempts, I am resorting to posting my question here. I have a pages controller as follows:

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>

当我访问页面索引时,我得到了nil:NilClass的未定义方法"each".该应用程序中还有其他控制器可以正常工作,与页面控制器没有明显区别.

I am getting undefined method 'each' for nil:NilClass when I visit the pages index. There are other controllers in the app which are working okay, without any noticeable difference from the pages controller.

任何指针都会有很大帮助.

Any pointers would be a great help.

推荐答案

取消对其中一个索引行的注释:

Uncomment one of your index lines:

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

更新

我对您的应用程序了解不多,但是您需要在使用@subject之前定义它,并且由于您的before_action :find_subject已被注释掉,因此您应该尝试取消对以下内容的注释:

I don't know much about your app but you'd need to define @subject before using it, and because your before_action :find_subject is commented out you should try uncommenting it from:

# before_action :find_subject

before_action :find_subject

ALSO

在视图中定义实例是一种不好的做法.该<% @page_title = "Pages" %>属于控制器索引,如下所示:

It's bad practice to define instances in views. This <% @page_title = "Pages" %> belongs in the controller index like so:

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

然后只需在您的视图中调用<%= @page_title %>.

Then just call <%= @page_title %> in your view.

这篇关于@pages实例变量为nil:NillClass-在Rails中构建一个简单的CMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:23