简体   繁体   中英

Why do I get an undefined method error for a nil:nilclass

so Im creating this rails app and I was able to add items to my app but now for some reason Im getting the error that I stated in the title. Please help me out to understand what Im doing wrong.

Error:

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

this is my _form_html.erb for Attraction View:

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

this is my 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

server log says the following

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'

The log says you are "in create", and rendering the 'new' template. That means your controller called its create method. This method does not define @categories ; hence when you pass it to f.select it can't call empty? on it.

Add an assignment for @categories to your create method.

Modify your partial as follows:

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

and remove assignment for variable @categories from the new and edit action

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM