ruby on rails - Comments to a specific product appear for all products -
hello developing demo ecommerce app, have created products, users , comments. each product page should show comments specific product. can comment product , can see comments product a, then, if check page of product b, see comments of product a. basically, comments mixed in long list instead of being sorted product... here github : https://github.com/adsidera/freshobst following partial products/_comment.html.erb comments
<div class="product-reviews"> <% @comments.each |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 sure?'} %> <i class="fa fa-trash-o fa-fw"></i> <% end %> </p> <% end %> </div> <% end %>
this 1 comment controller
class commentscontroller < applicationcontroller # admin abilities applied these. # 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 |format| if @comment.save format.html { redirect_to @product, alert: 'review created successfully'} format.json {render :show, status: :created, location: @product} else format.html { redirect_to @product, alert: 'review not 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 |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
and link products controller https://github.com/adsidera/freshobst/blob/master/app/controllers/products_controller.rb
thanks in advance help! anna
the problem @ show
method @ products controller, it's this:
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
when should like:
def show @product = product.find(params[:id]) @comments = @product.comments.paginate(:page => params[:page], :per_page => 3).order("created_at desc") end
Comments
Post a Comment