2016-02-07 85 views
1

您好,我正在開發演示電子商務應用程序,在那裏我創建了產品,用戶和評論。 每個產品頁面都應該顯示對特定產品的評論。 我可以對產品A進行評論,然後我可以看到產品A的所有評論,但是如果檢查產品B的頁面,我也會看到產品A的評論。所以基本上,所有評論都混合在一起在一個長長的清單,而不是被分類爲產品... 這裏是我的github:https://github.com/Adsidera/FreshObst 以下是部分產品/ _comment.html.erb徵求意見針對所有產品的特定產品的評論

<div class="product-reviews"> 

<% @comments.each do |comment| %> 

    <div class="row" style="padding-left:4%;"> 
     <HR> 
      <p><small><%= comment.user.first_name %><em><%= " #{time_ago_in_words(comment.created_at)} ago" %></em></small></p> 
      <div class="rated" data-score="<%= comment.rating %>"></div> 

      <p><%= comment.body %></p> 

      <% if signed_in? && current_user.admin? %> 
      <p><%= link_to product_comment_path(@product, comment), method: :delete, data: { confirm: 'Are you sure?'} do %> 
       <i class="fa fa-trash-o fa-fw"></i> 
       <% end %> 
      </p> 
      <% end %> 

    </div> 
<% end %> 

這一個是評論控制器

class CommentsController < ApplicationController 

# So admin abilities are applied to only these. 
# So public can view product without signing in. 
load_and_authorize_resource :only => [:destroy] 

def index 

end 

def create 
    @product = Product.find(params[:product_id]) 
    @comment = @product.comments.new(comment_params) 
    @comment.user = current_user 

    respond_to do |format| 
     if @comment.save 
      format.html { redirect_to @product, alert: 'Review was created successfully'} 
      format.json {render :show, status: :created, location: @product} 
     else 
      format.html { redirect_to @product, alert: 'Review could not be saved'} 
      format.json { render json: @comment.errors, status: :unprocessable_entity } 
     end 
    end 

end 

def destroy 
    @product = Product.find(params[:product_id]) 
    @comment = Comment.find(params[:id]) 
    product = @comment.product 
    @comment.destroy 
     respond_to do |format| 
      format.html {redirect_to @product, alert: 'Comment deleted successfully'} 
      format.json {render :show, location: @product} 
     end 
end 

def show 
end 

private 

def comment_params 
    params.require(:comment).permit(:user_id, :body, :rating) 
end 
end 

這是鏈接到我的產品控制器https://github.com/Adsidera/FreshObst/blob/master/app/controllers/products_controller.rb

預先感謝您的幫助! 安娜

回答

0

的問題是在在產品控制器show方法是這樣的:

def show 
     @product = Product.find(params[:id]) 
     # @comment = current_user.comment.build(comment_params) 
     @comments = @product.comments.order("created_at DESC") 
     @comments = Comment.paginate(:page => params[:page], :per_page => 3).order("created_at DESC") 
    end 

當它應該是這樣的:

def show 
     @product = Product.find(params[:id]) 
     @comments = @product.comments.paginate(:page => params[:page], :per_page => 3).order("created_at DESC") 
    end 
+0

謝謝!它確實有效! –