Rails app - 额外的记录显示

时间:2017-03-28 21:51:53

标签: ruby-on-rails ruby

我有一个应用程序,允许用户将他们喜欢的专辑添加到数据库中。然后任何人都可以来为该专辑撰写评论。很简单。

我遇到了一个问题,即每个专辑的展示页面上的评论都会显示额外的记录。每张专辑,即使它还没有被评论过,当我使用.each显示专辑节目页面中的每个评论时,还会显示另外一个看似空洞的评论。我想摆脱那个。

这是问题的图像。我用CSS来突出显示红色的评论。如您所见,底部有一个空的评论。当我检查相关评论时,它的标题是"尚未审核!"

enter image description here

这是我的albums_controller:

class AlbumsController < ApplicationController
  before_action :set_album, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, only: [:edit, :new, :update, :destroy]

  def index
    @albums = Album.all
    if params[:search].nil?
      @albums = Album.all.order(year: :desc).order(title: :asc).paginate(:page => params[:page], :per_page => 24)
    else
      @albums = @albums.where("albums.title || albums.year || albums.artist LIKE ?", "%#{params[:search]}%").order(year: :desc).order(title: :asc).paginate(:page => params[:page], :per_page => 24)
    end
  end

  def show
     if @album.reviews.blank?
      @average_review = 0
     else
      @average_review = @album.reviews.average(:rating).round(2)
     end
     @review = @album.reviews.build
   end

   def new
     @album = Album.new
   end

   def edit

   end

   def create
     @album = current_user.albums.build(album_params)

     respond_to do |format|
      if @album.save
         format.html { redirect_to @album, notice: 'Album was successfully created.' }
        format.json { render :show, status: :created, location: @album }
      else
        format.html { render :new }
        format.json { render json: @album.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
     respond_to do |format|
      if @album.update(album_params)
        format.html { redirect_to @album, notice: 'Album was successfully updated.' }
        format.json { render :show, status: :ok, location: @album }
      else
        format.html { render :edit }
        format.json { render json: @album.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @album.destroy
    respond_to do |format|
      format.html { redirect_to albums_url, notice: 'Album was successfully    destroyed.' }
      format.json { head :no_content }
    end
  end

private
# Use callbacks to share common setup or constraints between actions.

def set_album
  @album = Album.find(params[:id])
end

def album_params
  params.require(:album).permit(:title, :artist, :year, :cover)
end
end

这是专辑节目页面,这是显示额外评论的地方:

<div class="row">
<div class="col-xs-10 col-md-6 col-md-push-3 col-xs-push-1 top bottom">
    <%= image_tag @album.cover, class: 'show_image' %>
    <h2><%= @album.title %></h2>
    <h4><%= @album.artist %></h4>
    <h5><%= @album.year %></h5>
    <p class="white">Average Review<div class="average-review-rating" data-score=<%= @average_review %>></div></p>
    <% if user_signed_in? %>
        <% if @album.user_id == current_user.id %> 
            <%= link_to 'Edit', edit_album_path(@album), class: 'grey' %> 
            | <%= link_to 'Delete', album_path, method: :delete, data: {confirm: "Are you sure?"}, class: 'grey' %>
        <% end %> 
    <% end %>
    <br /><br />
    <h4>Reviews</h4>
    <% if user_signed_in? %>
        <p class="grey">Write a review</p> 

        <%= form_for [@album, @review] do |r| %>
            <div class="form-group">
                <div id="rating-form">
                    <label>Rating</label>
                </div>  
            </div>
            <div class="form-group">
                <%= r.text_area :comment, class: 'form-control', placeholder: "Write a comment" %>
            </div>
            <div class="form-group">
                <%= r.submit "Create", class: 'btn btn-success' %>
            </div>
        <% end %> 
    <% end %> 

    <br />
    <% @album.reviews.each do |r| %>
        <div class="red">
            <div class="review-rating" data-number="<%= r.rating %>">
            </div>
            <p class="white"><%= r.comment %></p>
            <% if user_signed_in? %>
                <% if current_user.id == r.user_id %> 
                    <%= link_to 'Edit', edit_album_review_path(@album, r.id), class: 'grey' %> |
                    <%= link_to 'Delete', album_review_path(@album, r.id), method: :delete, data: {confirm: "Are you sure?"}, class: 'grey' %>
                <% end %> 
            <% end %>
            <br /><br />
        </div>
    <% end %>
</div>
</div>



<script> 
$('.review-rating').raty({
readOnly: true,
score: function() {
    return $(this).attr('data-number');
},
path: '/assets/'
});

$('#rating-form').raty({
path: '/assets/',
scoreName: 'review[rating]'
});

$('.average-review-rating').raty({
readOnly: true,
path: '/assets/',
score: function() {
    return $(this).attr('data-score')
}
});
</script>

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

此处的问题出现在show方法的最后一行:@review = @album.reviews.build。此行不仅会创建一个新的Review实例并将其分配给@review,还会将该实例添加到@album.reviews中的数组中。因此,在您的视图中,当您遍历@album.reviews时,您将看到所有持久审核以及使用build构建的新审核,而非持久审核。

解决此问题的最简单方法是在视图中添加此行:

<% @album.reviews.each do |r| %>
  <% next unless r.persisted? %>

另一种解决方案是将新评论与相册相关联,但不会与@album实例相关联。在控制器中:

@review = Review.new(album: @album)

答案 1 :(得分:0)

我认为可能是因为show中的最后一行。

@review = @album.reviews.build

它构建了一个空的评论。