为什么我得到一个nil的未定义方法错误:nilclass

时间:2018-03-30 05:23:24

标签: ruby-on-rails ruby ruby-on-rails-3

所以我创建这个rails应用程序,我能够添加项目到我的应用程序但现在由于某种原因我得到我在标题中说明的错误。请帮助我了解我做错了什么。

错误:

<%= simple_form_for @attraction, :html => { :multipart => true} do |f| %>
  <%= f.select :category_id, @categories %>
  <%= f.input :name, label: "Name" %>
  <%= f.input :description %>
  <%= f.button :submit %>
<% end %>

这是吸引力视图的_form_html.erb:

<%= simple_form_for @attraction, :html => { :multipart => true} do |f| %>
  <%= f.select :category_id, @categories %>
  <%= f.input :name, label: "Name" %>
  <%= f.input :description %>
  <%= f.button :submit %>
<% end %>

这是我的attractions_controller_rb:

class AttractionsController < ApplicationController
  before_action :find_attraction, only: [:show, :edit, :update, :destroy]

  def index
    if params[:category].blank?
    @attractions = Attraction.all.order("created_at DESC")
    else
      @category_id = Category.find_by(name: params[:category]).id
      @attractions = Attraction.where(:category_id => @category_id).order("created_at DESC")
      end
  end

  def show
  end

  def new
    @attraction = current_user.attractions.build
    @categories = Category.all.map { |c| [c.name, c.id]}
  end

  def create
    @attraction = current_user.attractions.build(attraction_params)
    @attraction.category_id = params[:category_id]

    if @attraction.save
      redirect_to root_path
    else
      render 'new'
    end
  end


  def edit
    @categories = Category.all.map{ |c| [c.name, c.id]}
  end

  def destroy
    @attraction.destroy
    redirect_to root_path
  end


  def update
    @attraction.category_id = params[:category_id]
    if @attraction.update(attraction_params)
      redirect_to attraction_path(@attraction)
    else
      render 'edit'
    end

  end

  private
  def attraction_params
    params.require(:attraction).permit(:name, :description, :category_id)
  end

  def find_attraction
    @attraction = Attraction.find(params[:id])
  end

end

服务器日志说明如下

app/views/attractions/_form.html.erb:2:in `block in _app_views_attractions__form_html_erb__1839715867_118778080'
app/views/attractions/_form.html.erb:1:in `_app_views_attractions__form_html_erb__1839715867_118778080'
app/views/attractions/new.html.erb:3:in `_app_views_attractions_new_html_erb___877453770_118816340'
app/controllers/attractions_controller.rb:28:in `create'
Started POST "/attractions" for 127.0.0.1 at 2018-03-30 00:09:05 -0500
Processing by AttractionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"qvSzpGt+j9VOv212Vmm+cKeAgGFJghY2ri9HyeVtydW4cYcwROKjAvpUUFfEHvsEojfS+XA0Dk6pbeQMeQWaPg==", "attraction"=>{"category_id"=>"2", "name"=>"klk", "description"=>"klk"}, "commit"=>"Create Attraction"}
  [1m[36mUser Load (1.0ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?[0m  [["id", 1], ["LIMIT", 1]]
  [1m[35m (0.0ms)[0m  [1m[36mbegin transaction[0m
  [1m[35m (0.0ms)[0m  [1m[31mrollback transaction[0m
  Rendering attractions/new.html.erb within layouts/application
  Rendered attractions/_form.html.erb (18.8ms)
  Rendered attractions/new.html.erb within layouts/application (35.9ms)
Completed 500 Internal Server Error in 130ms (ActiveRecord: 1.0ms)



ActionView::Template::Error (undefined method `empty?' for nil:NilClass):
    1: <%= simple_form_for @attraction, :html => { :multipart => true} do |f| %>
    2:   <%= f.select :category_id, @categories %>
    3:   <%= f.input :name, label: "Name" %>
    4:   <%= f.input :description %>
    5:   <%= f.button :submit %>

app/views/attractions/_form.html.erb:2:in `block in _app_views_attractions__form_html_erb__1839715867_121522240'
app/views/attractions/_form.html.erb:1:in `_app_views_attractions__form_html_erb__1839715867_121522240'
app/views/attractions/new.html.erb:3:in `_app_views_attractions_new_html_erb___877453770_121587180'
app/controllers/attractions_controller.rb:28:in `create'

2 个答案:

答案 0 :(得分:1)

日志表示您处于“创建”状态,并呈现“新”模板。这意味着您的控制器调用了它的create方法。此方法未定义@categories;因此,当您将其传递给f.select时,它无法在其上调用empty?

@categories方法添加create的作业。

答案 1 :(得分:1)

修改您的部分,如下所示:

<%= simple_form_for @attraction, :html => { :multipart => true} do |f| %>
  <%= f.select :category_id, Category.all.map { |c| [c.name, c.id]} %>
  <%= f.input :name, label: "Name" %>
  <%= f.input :description %>
  <%= f.button :submit %>
<% end %>

并从新的和编辑操作中删除变量@categories的分配