2016-04-13 16 views
-3

nicht definierte Methode `map 'für nil: NilClasswenn ich eine Liste erschaffe, ich erhalte diesen Fehler

<% end %> 

    <div class="form-group"> 
    <%= select_tag(:location_id,options_for_select(@locations), :prompt=>"select your location")%> 
    </div> 

    <div class="form-group"> 
    <%= select_tag(:category_id,options_for_select(@categories), :prompt=>"select a category")%> 

listing_controller.rb

def new 
    @listing = Listing.new 
    @categories = Category.all.map{|c| [c.name, c.id]} 

    @locations= Location.all.map{|c| [c.name, c.id]} 
end 


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


def create 
    @listing = Listing.new(listing_params) 

    @listing.category_id = params[:category_id] 
    @listing.location_id = params[:location_id] 

    @listing.user_id = current_user.id 


    respond_to do |format| 
    if @listing.save 
     format.html { redirect_to @listing, notice: 'Listing was successfully created.' } 
     format.json { render :show, status: :created, location: @listing } 
    else 
     format.html { render :new } 
     format.json { render json: @listing.errors, status: :unprocessable_entity } 
    end 
    end 
end 
+0

Es scheint, als ob Sie keine Kategorie oder Listing-Instanzen haben. – Ilya

+0

Worauf bekommen Sie diesen Fehler? – Pavan

+0

Ich habe den Fehler behoben –

Antwort

0

nicht definierte Methode Karte für nil Klasse bedeutet, dass Sie' Rufen Sie die Methode auf etwas an, das null ist. Nil-Klasse hat diese Methode nicht.

Debug-Code, um herauszufinden, was das Objekt nil Klasse sein verursacht

0

Sie diesen Fehler entfernen können den Wert vor der Zuweisung Option Tag, wenn seine Null oder nicht durch den Code unten

<div class="form-group"> 
    <% unless @locations.empty? %> 
    <%= select_tag(:location_id,options_for_select(@locations), :prompt=>"select your location")%> 
    <% end %> 
</div> 

<div class="form-group"> 
    <% unless @categories.empty? %> 
    <%= select_tag(:category_id,options_for_select(@categories), :prompt=>"select a category")%> 
    <% end %> 
wählen
Verwandte Themen