2017-03-21 90 views
0

這是我的代碼我不斷收到錯誤「未定義的方法length' for nil:NilClass" even after I remove the line it gives me this error "undefined method每一個爲零:NilClass」我試圖顯示醫生列出的做法和他的服務在我的門戶上的評論`長度'爲零:NilClass

<div class="row"> 
    <div class="col-md-3"> 
    <div class="center"> 
     <%= image_tag avatar_url(@user), class: "avatar-full" %> 
     <% if current_user != @user %> 
     <div class="row-space-2"> 
      <%= link_to "Send Message", conversations_path(sender_id: current_user.id, recipient_id: @user.id), method: 'post', class: "btn btn-primary wide" %> 
     </div> 
     <% end %> 
    </div> 
    <div class="panel panel-default"> 
     <div class="panel-heading">Verification</div> 
     <div class="panel-body"> 
     Email Address<br> 
     Phone Number 
     </div> 
    </div> 
    </div> 

    <div class="col-md-9"> 
    <h2><%= @user.fullname %></h2> 

    <div class="description row-space-3"> 
     <%= @user.description %> 
    </div> 

    <h4>Listings (<%= @practices.length %>)</h4><br> 

    <div class="row"> 
     <% @practices.each do |practice| %> 
     <div class="col-md-4"> 
      <div class="panel panel-default"> 
      <div class="panel-heading preview"> 
       <%= image_tag room.photos[0].image.url(:medium) %> 
      </div> 
      <div class="panel-body"> 
       <%= link_to practice.speciality, practice %> 
      </div> 
      </div> 
     </div> 
     <% end %> 
    </div> 

    <h4>Reviews</h4><br> 

    <% @practices.each do |practice| %> 
     <% if !practice.reviews.blank? %> 
     <% practice.reviews.each do |review| %> 
      <div class="row"> 
      <div class="col-md-2 text-center"> 
       <%= image_tag avatar_url(review.user), class: "img-circle avatar-medium" %><br> 
       <%= review.user.fullname %> 
      </div> 
      <div class="col-md-10"> 
       <%= link_to room.listing_name, room %><br> 
       <%= review.comment %><br> 
       <%= review.created_at.strftime("%v") %> 
      </div> 
      </div> 
     <% end %> 

     <% end %> 
    <% end %> 
    </div> 
</div> 

請幫忙!我一直在堅持了幾天,出現此錯誤

這是我的控制器代碼:

class PracticesController < ApplicationController 
    before_action :set_practice, only: [:show,:edit,:update] 
    before_action :authenticate_user!,except: [:show] 

    def index 
    @practices = current_user.practices 
    end 

    def show 
    @photos = @practice.photos 
    @booked = Appointment.where("practice_id = ? AND user_id = ?",  @practice.id, current_user.id).present? if [email protected] = @practice.reviews 
    @hasReview = @reviews.find_by(user_id: current_user.id) if current_user 
    end 

    def new 
    @practice = current_user.practices.build 
    end 

    def create 
    @practice = current_user.practices.build(practice_params) 
    if @practice.save 
     if params[:images] 
      params[:images].each do |image| 
       @practice.photos.create(image: image) 
      end 
     end 
     @photos = @practice.photos 
     redirect_to edit_practice_path(@practice), notice: "Saved..." 
    else 
     render :new 
    end 
    end 

    def edit 
    if current_user.id == @practice.user.id 
     @photos = @practice.photos 
    else 
     redirect_to root_path, notice: "You don't have permission." 
    end 
    end 

    def update 
    if @practice.update(practice_params) 
     if params[:images] 
      params[:images].each do |image| 
       @practice.photos.create(image: image) 
      end 
     end 
     redirect_to edit_practice_path(@practice), notice: "Updated..." 
    else 
     render :edit 
    end 
    end 

    private 
    def set_practice 
    @practice = Practice.find(params[:id]) 
    end 

    def practice_params 
    params.require(:practice).permit(:dr_first_name,:dr_last_name,:experience,:speciality,:address,:professional_statement,:is_insurance,:insurance,:zip_code) 
    end 
end 

按照要求,我也貼在控制器的代碼

+0

'@ practices'似乎是'nil'。確保您正確地在您的控制器中加載了這些做法。 –

+0

可以請你把你的控制器代碼 – praga2050

+0

張貼控制器代碼 –

回答

0

使用try

<h4>Listings (<%= @practices.try(:length, 0) %>)</h4> 

它將在可用時返回長度,如果爲零,則返回0。

您也可以嘗試使用上的每個這樣的:

<% @practices.try(:each) do |practice| %> 

但在這種情況下,你可能想用if @practices.any?而不是圍繞着它,如果你想要的時候沒有顯示什麼特別的事實踐。

+0

不客氣。但請記住,如果你的變量不應該爲零,它不會再引發異常...所以你可能想確保這是預期的行爲... –

+0

謝謝!...我會牢記這一點! –

0

@practices變量必須是空的,所以更換

<h4>Listings (<%= @practices.length %>)</h4><br> 

<h4>Listings (<%= (@practices.to_s.empty?)? 'no length' : @practices.length %>)</h4> 

這將檢查@practices變量是否爲空,並阻止操作NilClass

+0

修復了列表問題 現在....它給這條線錯誤 <%@ practices.each do | practice | %> –

+0

所以問題是'@ practices'是空的,你也必須在這裏檢查,因爲現在它的調用'.each'方法在零。對你有意義嗎? –