Ruby on rails 如何在每个对象的索引页中显示平均评论评级
我有一个RubyonRails应用程序,应该可以帮助审查医院。 到目前为止,我遵循了一个详细的教程构建了一个相当可行的应用程序 我正在尝试向应用程序添加更多新功能Ruby on rails 如何在每个对象的索引页中显示平均评论评级,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个RubyonRails应用程序,应该可以帮助审查医院。 到目前为止,我遵循了一个详细的教程构建了一个相当可行的应用程序 我正在尝试向应用程序添加更多新功能 我希望用户能从主页上看到数据库中的医院列表,清楚地显示医院名称、图像和平均评论 我可以成功地在索引/主页上显示医院、图像和电话号码,但很难显示每家医院所有评论的平均评分。 这很奇怪,因为我在各个医院的展示页面上有完全相同的结构,而且在那里效果很好 我觉得我就在几步之外,需要帮助。请参阅下面的代码片段: 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>“Explore the best of healthcare available in your community”</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>
<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>
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
<h3>
<%= @hospital.name %>
</h3>
<%= @hospital.avg_rating %>
<p><%= "#{@reviews.length} reviews" %></p>
搜寻
还没有评论。第一个写一篇!
|
美元(“.星级”).raty({
路径:'https://s3-us-west-2.amazonaws.com/morafamedapp/stars',
只读:对,
分数:函数(){
返回$(this.attr('data-score');
}
});
医院.rb
<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>“Explore the best of healthcare available in your community”</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>
<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>
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
<h3>
<%= @hospital.name %>
</h3>
<%= @hospital.avg_rating %>
<p><%= "#{@reviews.length} reviews" %></p>
一级医院
show.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>“Explore the best of healthcare available in your community”</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>
<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>
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
<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>
Showing/Users/oluwaseunmorafa/medaapp1/app/views/hospitals/show.html.erb,其中第9行出现:
未定义的方法“平均额定值”#
提取的源(第9行附近):
7.
8.
9
10
11
12
在您的医院
类中,您定义了一个类方法而不是实例方法。因此,删除self.avg_rating
方法,改为编写:
def avg_rating
review.average(:rating)
end
然后在您的视图中(在show And index中),您只需参考
hospital.avg_rating
即可显示评级 看起来您已经包含了大量不必要的代码,并且省略了许多实际相关的代码。您的问题应该包含一个包含重现问题所需的最少代码的问题。您正在询问索引
视图的问题,但您的问题中没有包含该视图。也没有理由包括整个审查控制器。请编辑您的问题以删除不相关的代码,并包括您为索引视图编写的任何代码,即使它当前不起作用,以及它生成的任何错误。快速回答:按医院id进行分组审核
,然后为每个集合计算其平均值
。不那么快的答案是:这不是很好的规模。如果您计划扩展此应用程序以显示许多医院的平均评论,那么您应该将结果缓存在某个位置,而不是每次加载页面时重新计算。但如果这只是一个快速的小实验项目,那么别担心。嗨@oluwaseun morafa,你给了我们这么多信息真是太棒了。然而,这让你的问题很难理解。最好将重点放在问题领域,并使之更清楚。如果我没看错的话,你在展示一家医院的时候有问题,然后再展示?精明的?平均评论?哈,等等,在你编辑之后,我理解了问题:你想像在显示页面上那样在索引视图中显示平均值吗?(它在哪里工作?)@nathanvda。在您的show视图中,您可以计算平均评级
,但您必须对索引中的每一行进行计算。我会将avg_评级
的计算移动到Hospital
类,然后您可以只编写Hospital.avg_评级
,它将在索引和显示页面中工作。