NoMethodError在第#编辑类似的问题与其他(NoMethodError in Sectio

2019-10-20 07:28发布

我是一个有点新的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

这里是我的窗体页:

<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>

这里是我的控制器:

 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

这里的视图页面(编辑):

<% @page_title = "Edit Section" %>

<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>

<div class="sections edit">
  <h2>Update Section</h2>

  <%= form_for(:section, :url => {:action => 'update', :id => @section.id}) do |f| %>

    <%= render(:partial => "form", :locals => {:f => f}) %>

    <div class="form-buttons">
      <%= submit_tag("Update Section") %>
    </div>

  <% end %>
</div>

Answer 1:

你应该定义@pages@section_count在适当的控制器的操作变量:

def edit
  @section = Section.find(params[:id])
  @pages = Page.order('position ASC')
  @section_count = Section.count
end

你也有一个错误在创建行动。 当它没有创造一项纪录, @section_countSection.count + 1作为new行动。

你的控制器可能也折射到这个



文章来源: NoMethodError in Sections#edit similar issue to other