Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails `长度';零级:零级_Ruby On Rails - Fatal编程技术网

Ruby on rails `长度';零级:零级

Ruby on rails `长度';零级:零级,ruby-on-rails,Ruby On Rails,这是我的代码,我一直在为nil:NilClass获取错误“undefined methodlength” 即使在我删除该行之后,它也会给我这个错误“undefined methodeach'for nil:NilClass”。我正试图在我的门户上显示医生列出的实践和对他的服务的评论 <div class="row"> <div class="col-md-3"> <div class="center"> <%= image_tag

这是我的代码,我一直在为nil:NilClass获取错误“undefined method
length”
即使在我删除该行之后,它也会给我这个错误“undefined method
each'for nil: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 current_user@reviews = @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
class PracticesController

根据要求,我还发布了控制器的代码

您的
@practices
变量必须为空,因此请替换

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

清单()
这将检查
@practices
变量是否为空,并防止对
NilClass
使用
try
执行
.length
操作

<h4>Listings (<%= @practices.try(:length, 0) %>)</h4>
清单()
如果可用,它将返回长度,如果为零,则返回0

您也可以像下面这样使用“试穿”功能:

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


但在本例中,您可能希望在它周围加上一个
if@practices.any?
,以防在没有实践的情况下显示某些特定内容。

@practices
似乎是
nil
。请确保在控制器中正确加载练习。请将控制器代码放入已修复列表问题的控制器代码中。此行的给定错误,因此问题是
@practices
为空,您也必须在此处检查,因为现在它调用了nil上的每个
方法。你明白吗?不客气。但请记住,如果您的变量不应为零,它将不再引发异常。。。因此,您可能希望确保这是预期的行为。。。
<% @practices.try(:each) do |practice| %>