2017-03-21 87 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'方法在零。对你有意义吗? –