如何在每个对象的索引页面中显示平均评论评分

时间:2018-05-10 13:50:54

标签: ruby-on-rails ruby

我有一个Ruby on Rails应用程序,应该有助于审查医院。 到目前为止,我遵循了一个关闭的教程来构建一个相当可行的应用程序。

我正在尝试为应用添加更多新功能

  1. 我希望用户在主页上查看数据库中的医院列表,清楚地显示医院名称,图片和平均评价
  2. 我可以在索引/家庭用户上成功显示医院,图片和电话号码,但很难显示每家医院所有评论的平均评分。 这很奇怪,因为我在各个医院的展示页面上都有完全相同的结构,并且在那里工作正常。

    我觉得我只是几步之遥,需要帮助。请参阅以下代码段:

    index.html.erb

    <div class="jumboFluid">
      <div class="jumbotron">
        <section class="content">
          <%= form_tag search_hospitals_path, method: :get, class: "form-group" do %>
            <div class="input-group">
              <div class="input-group-addon">Search</div>
                <p>
                  <%= text_field_tag :search, params[:search],  class: "form-control formInput", placeholder: "Eye, Maternity" %>
                  <%# <%= submit_tag "Search", name: nil, class: "btn btn-default" %>
                </p>
              </div>
            </div> 
          <% end %>
        </section>
      </div>
    </div>
    <div class="hospitalList">
      <h1 id="hospitalBanner">Hospitals</h1>
      <blockquote> 
        <p class="text-center"><cite>&#8220;Explore the best of healthcare available in your community&#8221;</cite> </p>
      </blockquote>
    
      <% content_for(:body_attributes) do %>
        data-no-turbolink="false"
      <% end %>
    
      <div class="container-fluid">
        <div class="row">
          <% @hospitals.each do |hospital| %>
            <div class="col-md-3">
              <div class="thumbnail">
                <%= link_to image_tag(hospital.image), hospital %>
                <div class="caption">
                  <p> <%= link_to hospital.name, hospital %></p>
                  <p> <%= hospital.phone %></p>
                </div>
              </div>
            </div>
          <% end %>
        </div>
    
        <br>
    
        <% if user_signed_in? && current_user.admin? %>
          <%= link_to 'New Hospital', new_hospital_path, class: "btn btn-link" %>
        <% end %>
    
    <script>
      $('.star-rating').raty({
        path: 'https://s3-us-west-2.amazonaws.com/morafamedapp/stars',
        readOnly: true,
        score: function() {
          return $(this).attr('data-score');
        }
      });
    </script>
    
    </div>
    

    工作展示代码

    show.html.erb

    <div class="row">
      <div class="col-md-3">
        <%= image_tag @hospital.image_url unless @hospital.image.blank? %>
    
        <h3>
          <%= @hospital.name %>
        </h3>
    
        <div class="star-rating" data-score= <%= @avg_rating %> ></div>
        <p><%= "#{@reviews.length} reviews" %></p>
        <%= social_share_button_tag("Share") %>
    
    
        <p>
          <strong>Address:</strong>
          <%= @hospital.address %>
        </p>
    
        <p>
          <strong>Phone:</strong>
          <%= @hospital.phone %>
        </p>
    
        <p>
          <strong>Website:</strong>
          <%= link_to @hospital.website, @hospital.website, target: :_blank %>
        </p>
    
        <%= link_to "Write a Review", new_hospital_review_path(@hospital), class: "btn btn-primary" %>
    
        <br>
        <br>
    
        <iframe width="230" height="230" frameborder="0" style="border:0"
      src="https://www.google.com/maps/embed/v1/place?q=<%= @hospital.address.parameterize %>&key=AIzaSyAc4mTzAGIA_8JFXAcL3XTBi-tzuxQCsBc" allowfullscreen></iframe>
      </div>
    
      <div class="col-md-9">
        <% if @reviews.blank? %>
          <h3>No reviews yet. Be the first to write one!</h3>
        <% else %>
          <table class="table">
            <thead>
              <tr>
                <th class="col-md-3"></th>
                <th class="col-md-9"></th>
              </tr>
            </thead>
    
            <tbody>
              <% @reviews.each do |review| %>
                <tr>
                  <td>
                    <h4>
                      <%= "#{review.user.first_name.capitalize} #{review.user.last_name.capitalize[0]}." %>
                    </h4>
                    <p><%= review.created_at.strftime("%-d/%-m/%y") %></p>
                  </td>
    
                  <td>
                    <div class="star-rating" data-score= <%= review.rating %> ></div>
                    <p><%= h(review.comment).gsub(/\n/, '<br/>').html_safe %></p>
                    <%= social_share_button_tag("Share") %>
    
                    <% if user_signed_in? %>
                      <% if (review.user == current_user) || (current_user.admin?) %>
                      <%= link_to "Edit", edit_hospital_review_path(@hospital, review) %>
                      <%= link_to "Delete", hospital_review_path(@hospital, review), method: :delete %>
    
                    <% end %>
                  <% end %>
                  </td>
                </tr>
              <% end %>
            </tbody>
          </table>
        <% end %>
      </div>
    </div>
    
    <%= link_to 'Edit', edit_hospital_path(@hospital), class: "btn btn-link" %> |
    <%= link_to 'Back', hospitals_path, class: "btn btn-link" %>
    
    <script>
      $('.star-rating').raty({
        path: 'https://s3-us-west-2.amazonaws.com/morafamedapp/stars',
        readOnly: true,
        score: function() {
          return $(this).attr('data-score');
        }
      });
    </script>
    

    hospital.rb

    class Hospital < ApplicationRecord
      mount_uploader :image, ImageUploader
    
      searchkick
    
      has_many :reviews
    
      def self.import(file)
        CSV.foreach(file.path, headers: true) do |row|
          Hospital.create! row.to_hash
        end
      end 
    
      def self.avg_rating
        Hospital.avg_rating
      end
    
    
    end
    

    show.html.erb

        <h3>
          <%= @hospital.name %>
        </h3>
    
        <%= @hospital.avg_rating %>
    
        <p><%= "#{@reviews.length} reviews" %></p>
    

    错误

    Showing /Users/oluwaseunmorafa/medaapp1/app/views/hospitals/show.html.erb where line #9 raised:
    undefined method `avg_rating' for #<Hospital:0x007fd846496ee0>
    Extracted source (around line #9):
    7
    8
    9
    10
    11
    12
    
        </h3>
    
        <%= @hospital.avg_rating %>
    
        <p><%= "#{@reviews.length} reviews" %></p>
    

1 个答案:

答案 0 :(得分:0)

Hospital类中,您定义了一个类方法而不是实例方法。因此,请删除self.avg_rating方法,然后写:

  def avg_rating
    review.average(:rating)
  end 

然后在您的观看中(在展示和索引中),您可以参考hospital.avg_rating来显示评分。

相关问题