未定义的方法`collect'for nil:NilClass [英] undefined method `collect' for nil:NilClass

查看:177
本文介绍了未定义的方法`collect'for nil:NilClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解决这个错误吗?


NoMethodError in posts#create

NoMethodError in Posts#create

显示C:/Users/User/Documents/website/app/views/posts/_form.html.erb
其中第24行出现:

Showing C:/Users/User/Documents/website/app/views/posts/_form.html.erb where line #24 raised:

未定义的方法`collect'for nil:NilClass

undefined method `collect' for nil:NilClass

提取的来源(约
行#24):

Extracted source (around line #24):

23:<%= f.label:categoria
%>
24:<%= f.select:categoria_id,@ categoria.collect {| c | [c.name,c.id]}%>

23: <%= f.label :categoria %>
24: <%= f.select :categoria_id, @categoria.collect { |c| [ c.name, c.id ] } %>

包含模板的跟踪:app / views / posts / new.html.erb

Trace of template inclusion: app/views/posts/new.html.erb

我的_form.html.erb

My _form.html.erb

<%= form_for(@post, :html => { :multipart => true }) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :titulo %><br />
    <%= f.text_field :titulo %>
  </div>
  <div class="field">
    <%= f.label :conteudo %><br />
    <%= f.text_area :conteudo %>
  </div>
  <div class="field">
    <%= f.label :categoria %><br />
     <%= f.select :categoria_id, @categoria.collect { |c| [ c.name, c.id ] } %>
  </div>
  <br />
  <div class="field">
  <%= f.file_field :avatar %>
  </div>
  <br />
  <div class="actions">
    <%= f.submit %>
  </div>
  <br />
<% end %>

我的控制器:

class PostsController < ApplicationController
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/new
  # GET /posts/new.json
  def new
    @post = Post.new
    @categoria = Categorium.all
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
    @categoria = Categorium.all
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /posts/1
  # PUT /posts/1.json
  def update
    @post = Post.find(params[:id])

    respond_to do |format|
      if @post.update_attributes(params[:post])
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
end


推荐答案

您的操作正在定义

Your new action is defining @categoria, which is then used in your view.

如果您的 create action创建一个无效对象,它将重新渲染仍然期望定义 @categoria 视图,但是

If your create action creates an invalid object, it will re-render the new view which still expects @categoria to be defined, but it is not.

您需要在 new 创建操作。

这篇关于未定义的方法`collect'for nil:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆