【问题标题】:undefined method for collect error收集错误的未定义方法
【发布时间】:2016-05-01 17:31:13
【问题描述】:

问候我的程序员伙伴。我有 2 个问题。 1当然是我该如何解决这个问题。其次,这个错误是什么意思,所以对于我的下一次遭遇,我知道如何处理它。

我的 _form.html.erb

<%= error_messages_for(@section) %>

  <table summary="Section form fields">
      <tr>
        <th><%= f.label(:page_id, "Page ID") %></th>
         <td><%= f.select(:page_id, @pages.collect {|p| [p.name, p.id]}) %>
      </tr>
      <tr>
        <th><%= f.label(:name) %></th>
        <td><%= f.text_field(:name) %></td>
      </tr>
      <tr>
        <th><%= f.label(:position) %></th>
        <td><%= f.text_field(:position) %></td>
      </tr>
      <tr>
        <th><%= f.label(:visible) %></th>
        <td><%= f.select(:visible, 1..@section_count) %></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"

  before_action :confirm_logged_in
  before_action :find_page

  def index
    @sections = @page.sections.sorted
  end

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

  def new
    @section = Section.new({:name => "Default", :page_id => @page.id})
    @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', :page_id => @page.id)
    else
      @pages = @page.subject.pages.sorted
      @section_count = Section.count +1
      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, :page_id => @page.id)
    else
      @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' , :page_id => @page.id)
  end

  private

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

  def find_page
    if params[:page_id]
      @page = Page.find(params[:page_id])
    end
  end
end

【问题讨论】:

  • 嘿,将@pages = @page.subject.pages.sorted 添加到您的new 操作中,因为@pages 在new 中为零,所以它会给您这个错误
  • @VishalJAIN 我试图让您的评论成为主要答案。你的声望低吗?
  • @VishalJAIN 必须写下答案

标签: ruby-on-rails class methods null


【解决方案1】:

嘿,您试图在表单上的 @pages 对象上调用 collect 方法,该对象是 nil

所以将新操作中的代码替换为

def new
    @section = Section.new({:name => "Default", :page_id => @page.id})
    @section_count = Section.count +1
    @pages = @page.subject.pages.sorted
end

现在@pages 不为空,因此您可以对其调用collect 方法。

【讨论】:

    【解决方案2】:

    接受@VishalJAIN 的回答,这只是您第二个问题的插件。


    错误

    每个计算机错误都是特定的; 80% 的战斗识别错误本身。

    你的错误在 Rails 中比较常见:

    未定义的方法 for ...

    这意味着您正在调用解释器无法识别的“方法”:

    @pages.collect {|p| [p.name, p.id]})
    

    ...@pages 要么没有 collect 方法,要么你的 @pages 变量未定义。

    正如 cmets 中所指出的,在您的 new 操作中添加 @pages = ... 可以解决问题。

    --

    原因

    错误的原因是,由于 Ruby 以及 Rails 的优点是 object orientated,因此每次调用变量/方法时,Ruby 需要使其可用。

    在大多数语言中,您会收到“变量未声明”错误——变量只有在定义后才能被引用; Ruby 将所有内容放入对象

    undefined method 'collect' for "nil:NilClass"
    

    如果您在应用程序中看到上述错误,则表示您尚未声明要引用的变量。

    【讨论】:

    • 感谢您的详细解释,我一直喜欢您的回答
    • 谢谢!很高兴能写出来;你应该接受@VishalJAIN 的回答,因为他在 cmets 中是正确的
    猜你喜欢
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    相关资源
    最近更新 更多