undefined method `collect' for nil:NilClass

2019-04-10 14:44发布

Can someone help me with this error?

NoMethodError in Posts#create

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

undefined method `collect' for nil:NilClass

Extracted source (around line #24):

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

Trace of template inclusion: app/views/posts/new.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 %>

My controller:

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

5条回答
Ridiculous、
2楼-- · 2019-04-10 15:03
<%= f.select :categoria_id, options_for_select(@categoria, :name, :id), :required => true %>
查看更多
神经病院院长
3楼-- · 2019-04-10 15:05

Where's your create action? You have defined a new action but no create action in your controller. The create action is what will save your object.

查看更多
小情绪 Triste *
4楼-- · 2019-04-10 15:05

Have you created any categoria yet? That would be the only reason I can think of why @categoria would return nil in your view.

查看更多
地球回转人心会变
5楼-- · 2019-04-10 15:14

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

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.

You will need to define the instance variable in both your new and create actions.

查看更多
我想做一个坏孩纸
6楼-- · 2019-04-10 15:14

You have to define @categoria = Categoria.all instance variable in both new, create, edit, update actions in your controller.

查看更多
登录 后发表回答