NoMethodError in Sections#edit similar issue to ot

2019-08-05 09:30发布

问题:

I am a bit new to Ruby and am writing a program from scratch... I'm getting an error and I can't figure out where it is coming from. I've spent over an hour and i'm sure it's something stupidly simple. I have other similar pages that work fine, but there's some issue with the instance variable. It's almost an identical problem as the link provided.

Here's my error:

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

Here's my forms page:

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

Here's my controller:

 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

Here's the view page (edit):

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

回答1:

You should define @pages and @section_count variables in appropriate controller action:

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

You also have a mistake in create action. When it doesn't create a record, @section_count should be Section.count + 1 as in new action.

Your controller could be also refractor to this