首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使平均评等显示在每个对象的索引页中

如何使平均评等显示在每个对象的索引页中
EN

Stack Overflow用户
提问于 2018-05-10 13:50:54
回答 1查看 400关注 0票数 0

我有一个Rails应用程序,应该是帮助审查医院。到目前为止,我遵循了一个紧密的教程来构建,并且有一个相当可行的应用程序。

我正在尝试给这个应用程序添加一些新的功能。

  1. 我希望用户能在首页的数据库中看到一份医院列表,清楚地显示医院的名称、形象和平均评价。

我可以成功地显示医院,图像和电话号码的索引/家庭pag,但努力显示平均评分的所有每家医院。这是奇怪的,因为我有完全相同的结构,在个别医院显示页,它在那里工作良好。

我觉得我离这里只有几步之遥,需要帮助。请参阅下面的代码片段:

index.html.erb

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
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

代码语言:javascript
复制
    <h3>
      <%= @hospital.name %>
    </h3>

    <%= @hospital.avg_rating %>

    <p><%= "#{@reviews.length} reviews" %></p>

误差

代码语言:javascript
复制
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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-11 16:23:14

Hospital类中,您定义了类方法,而不是实例方法。因此,移除self.avg_rating方法,而不是写:

代码语言:javascript
复制
  def avg_rating
    review.average(:rating)
  end 

然后在视图中(在显示和索引中),只需参考hospital.avg_rating来显示评级。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50274508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档