Ruby 章节中的命名错误#编辑与其他类似的问题

Ruby 章节中的命名错误#编辑与其他类似的问题,ruby,class,methods,ruby-on-rails-4.1,Ruby,Class,Methods,Ruby On Rails 4.1,我对Ruby有点陌生,正在从头开始写一个程序。。。我遇到了一个错误,我不知道它是从哪里来的。我已经花了一个多小时,我相信这是一件非常简单的事情。我有其他类似的页面可以正常工作,但实例变量存在一些问题。这几乎是一个和我一样的问题 这是我的错误: Showing C:/Users/kevin/Desktop/ruby_test/sites/simple_cms/app/views/sections/_form.html.erb where line #4 raised: undefined

我对Ruby有点陌生,正在从头开始写一个程序。。。我遇到了一个错误,我不知道它是从哪里来的。我已经花了一个多小时,我相信这是一件非常简单的事情。我有其他类似的页面可以正常工作,但实例变量存在一些问题。这几乎是一个和我一样的问题

这是我的错误:

Showing C:/Users/kevin/Desktop/ruby_test/sites/simple_cms/app/views/sections/_form.html.erb where line #4 raised:

    undefined method `collect' for nil:NilClass
    Extracted source (around line #4):

   1   <table summary="Section form fields">
   2     <tr>
   3       <th><%= f.label(:page_id, "Page") %></th>
   4       <td><%= f.select(:page_id, @pages.collect {|p| [p.name, p.id]}) %>
   5   </td>
   6     </tr>
   7     <tr>

    Trace of template inclusion: app/views/sections/edit.html.erb

    Rails.root: C:/Users/kevin/Desktop/ruby_test/sites/simple_cms
显示C:/Users/kevin/Desktop/ruby_test/sites/simple_cms/app/views/sections/_form.html.erb,其中第4行出现:
nil:NilClass的未定义方法“collect”
提取的源(第4行附近):
1.
2.
3.
4.
5.
6.
7.
模板包含跟踪:app/views/sections/edit.html.erb
root:C:/Users/kevin/Desktop/ruby\u test/sites/simple\u cms
这是我的表格页面:

<table summary="Section form fields">
  <tr>
    <th><%= f.label(:page_id, "Page") %></th>
    <td><%= f.select(:page_id, @pages.collect {|p| [p.name, p.id]}) %>
</td>
  </tr>
  <tr>
    <th><%= f.label(:name) %></th>
    <td><%= f.text_field(:name) %></td>
  </tr>
  <tr>
    <th><%= f.label(:position) %></th>
    <td><%= f.select(:position, 1..@section_count) %></td>
  </tr>
  <tr>
    <th><%= f.label(:visible) %></th>
    <td><%= f.check_box(:visible) %></td>
  </tr>
  <tr>
    <th><%= f.label(:content_type) %></th>
    <td>
      <%= f.radio_button(:content_type, 'text') %> Text
      <%= f.radio_button(:content_type, 'HTML') %> HTML
    </td>
  </tr>
  <tr>
    <th><%= f.label(:content) %></th>
    <td><%= f.text_area(:content, :size => '40x10') %></td>
  </tr>
</table>

正文
HTML
“40x10”)%%>
这是我的控制器:

 class SectionsController < ApplicationController

      layout "admin"

      def index
        @sections = Section.sorted
      end

      def show
        @section = Section.find(params[:id])
      end

      def new
        @section = Section.new({:name => "Default"})
        @pages = Page.order('position ASC')
        @section_count = Section.count + 1
      end

      def create
        @section = Section.new(section_params)
        if @section.save
          flash[:notice] = "Section created successfully."
          redirect_to(:action => 'index')
        else
          @pages = Page.order('position ASC')
          @section_count = Section.count
          render('new')
        end
      end

      def edit
        @section = Section.find(params[:id])
      end

      def update
        @section = Section.find(params[:id])
        if @section.update_attributes(section_params)
          flash[:notice] = "Section updated successfully."
          redirect_to(:action => 'show', :id => @section.id)
        else
          @pages = Page.order('position ASC')
          @section_count = Section.count
          render('edit')
        end
      end

      def delete
        @section = Section.find(params[:id])
      end

      def destroy
        section = Section.find(params[:id]).destroy
        flash[:notice] = "Section destroyed successfully."
        redirect_to(:action => 'index')
      end


      private

        def section_params
          params.require(:section).permit(:page_id, :name, :position, :visible, :content_type, :content)
        end

    end
类节控制器“Default”})
@页码=页码顺序('位置ASC')
@截面计数=截面计数+1
结束
def创建
@截面=截面。新建(截面参数)
如果@section.save
flash[:notice]=“已成功创建节。”
将_重定向到(:action=>'index')
其他的
@页码=页码顺序('位置ASC')
@section\u count=section.count
呈现('新')
结束
结束
定义编辑
@section=section.find(参数[:id])
结束
def更新
@section=section.find(参数[:id])
如果@section.update_属性(section_参数)
flash[:notice]=“节已成功更新。”
重定向到(:action=>'show',:id=>@section.id)
其他的
@页码=页码顺序('位置ASC')
@section\u count=section.count
渲染('编辑')
结束
结束
def删除
@section=section.find(参数[:id])
结束
def销毁
section=section.find(params[:id]).destroy
flash[:notice]=“部分已成功销毁。”
将_重定向到(:action=>'index')
结束
私有的
def截面参数
参数require(:section).permit(:页面id,:名称,:位置,:可见,:内容类型,:内容)
结束
结束
以下是查看页面(编辑):


'index'},:class=>'backlink')%>
更新部分
{:action=>'update',:id=>@section.id})do | f |%>
“form”,:locals=>{:f=>f})%>

您应该在适当的控制器操作中定义
@页面
@节计数
变量:

def edit
  @section = Section.find(params[:id])
  @pages = Page.order('position ASC')
  @section_count = Section.count
end
您在创建操作中也有错误。当它不创建记录时,
@section\u count
应该是
section.count+1
,就像在
new
操作中一样


您的控制器也可能是折射器,我想我理解。因此,我需要声明变量或将其设置为“nil”以便将其传递给编辑函数(函数| |=)的目的,这样的答案是否有效?你说的以部分形式通过是什么意思?我能够得到您关于使用第二组代码的建议,而不需要您提供的第一个代码部分。我还可以通过在sections控制器的顶部设置'code'@pages=Page.order('position ASC')&@section\u count=section.count'code'来解决这个问题。这样合适吗?我抓住了+1,谢谢你指出了这一点。是的,我认为不止如此,没有必要这么做。只要创建变量,它们就可以在局部视图中使用。你说得对。这与你的问题无关,但对于你的表单,我建议使用一个名为simple_form by platformatec的ruby gem。这是为应用程序生成表单的一种非常方便的方法:)
def edit
  @section = Section.find(params[:id])
  @pages = Page.order('position ASC')
  @section_count = Section.count
end