2016-04-24 5 views
2

Ich mache Suche. Aber ich bekomme Fehler bei each in der SuchansichtWarum bekomme ich undefinierte Methode `each` für nil: NilClass in Rails?

each für Ansichten/Gehäuse/index.html.erb funktioniert gut.

Aber unten ist views/search/search_housing.html.erb wehre ich each bin immer:

<tbody> 
    <% @housings.each do |housing| %> 
    <tr> 
    <td><%=link_to "#{housing.title}", housing_path(housing.slug) %></td> 
    <td><%= housing.category.name %></td> 

unten ist mein Gehäuse Controller

class HousingsController < ApplicationController 
    before_action :set_housing, only: [:show, :edit, :update, :destroy] 

    # GET /housings 
    # GET /housings.json 
    def index 
    @housings = Housing.all.order(created_at: :desc).paginate(page: params[:page], per_page: 10) 
    end 

    # GET /housings/1 
    # GET /housings/1.json 
    def show 
    end 

    # GET /housings/new 
    def new 
    @housing = Housing.new 
    end 

    # GET /housings/1/edit 
    def edit 
    if not @housing.user_email == current_user.email || current_user.email == "[email protected]" 
     redirect_to @housing 
    end 
    end 

    # POST /housings 
    # POST /housings.json 
    def create 
    @housing = Housing.new(housing_params) 
    @housing.user_email = current_user.email 

    respond_to do |format| 
     if @housing.save 
     format.html { redirect_to @housing } 
     flash[:success] = "Housing was successfully created." 
     else 
     format.html { render :new } 
     format.json { render json: @housing.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /housings/1 
    # PATCH/PUT /housings/1.json 
    def update 
    respond_to do |format| 
     if @housing.update(housing_params) 

     format.html { redirect_to @housing } 
     format.json { render :show, status: :ok, location: @housing } 
     flash[:success] = "Housing was successfully updated." 
     else 
     format.html { render :edit } 
     format.json { render json: @housing.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /housings/1 
    # DELETE /housings/1.json 
    def destroy 
    @housing.destroy 
    respond_to do |format| 
     format.html { redirect_to housings_url } 
     format.json { head :no_content } 
     flash[:alert] = "Housing was successfully destroyed." 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_housing 
     @housing = Housing.friendly.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def housing_params 
     params.require(:housing).permit(:title, :type, :description, :location, :user_email, :created_at, :category_id, :slug) 
    end 
end 

Unten ist mein Suchsteuerung

class SearchController < ApplicationController 
      def search_housing 
        @housings = Housing.search((params[:search].present? ? params[:search] : '*')).records.order(created_at: :desc) 
        # if params[:search].nil? 
        #  @housings = Housing.all.order(created_at: :desc) 
        # else 
        #  @housings = Housing.search params[:search] 
        # end 
       end 
    end 
+0

Funktioniert es in Ihrem Search Controller, wenn Sie die Zeile '@housings = Housing.all.order (created_at:: desc)' alleine auskommentieren? –

Antwort

0

Dieser Fehler herkommt Ihre Ansicht:

<% @housings.each do |housing| %> 

während Sie versuchen, auf @housing zuzugreifen, was null ist.

Try

raise Ausnahme oder binding.pry Debugger in Ihrem Controller zu setzen und überprüfen, was das Ergebnis der Abfrage ist.

class SearchController < ApplicationController 
     def search_housing 
       @housings = Housing.search((params[:search].present? ? params[:search] : '*')).records.order(created_at: :desc) 
       binding.pry   
     end 
end 

Ich nehme Ihre Anfrage für Housing.search gibt nil zurück.

Um binding.pry Debugger Prüfung Link https://github.com/rweng/pry-rails

Beifall zu installieren.

0

OH MAY GOD, fand ich übrigens .. Ich habe vergessen, nur ein end Ende des Such Controller zu setzen ...

Danke Jungs!

+0

Sie hätten einen SyntaxError bekommen, wenn das der Fall wäre, also war es definitiv etwas anderes. Froh, dass du es repariert hast! –

Verwandte Themen