2016-04-24 47 views
2

我在做搜索。但我在搜索視圖each錯誤我是否得到未定義的方法`each'for nil:NilClass in Rails?

each for views/housing/index.html.erb工作正常。

但下面是views/search/search_housing.html.erb wehre我得到each

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

是我的房屋控制器

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 

下面是我的搜索控制器

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

在您的搜索控制器中,當您單獨註釋'@housings = Housing.all.order(created_at::desc)'行時它是否工作? –

回答

0

這個錯誤來自你查看:

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

當你試圖訪問@房屋是無。

儘量把

引發異常或binding.pry調試器在你的控制器,檢查什麼是您的查詢的結果。

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

我想你的查詢Housing.search返回nil。

要安裝binding.pry調試器檢查此鏈接https://github.com/rweng/pry-rails

乾杯。

0

OH願上帝,我發現方式.. 我剛纔忘了把搜索控制器的end結束...

謝謝你們!

+0

如果情況確實如此,你將得到一個SyntaxError,所以它絕對是別的。很高興你解決它,但! –

相關問題