0

I have a simple app that lets users upload products and then comment on those products.

I currently have the products #show page listing the associated comments for that product. So that part works. What I cant seem to get working is, I would like to show comments for each product on the products #index page.

I was thinking that what I have working for the products#show view would also work for the #index view, but that does not seem to be the case in my situation.

comments controller

def create
  @product = Product.find(params[:product_id])
  @comment = @product.comments.build(params[:comment])
  @comment.user = current_user
  if @comment.save
    redirect_to @product, notice: "Comment was created."
  else
    render :new
  end
end

Products Controller (I'm not sure what I should be putting in the index action here. Nothing I have tried works. and @comments = @product.comments throws a noMethod error on "comments" on the productController#index action)

def index
  @products = Product.all(:include => :comments)
  #something about comments, nothing I've tried so far has worked
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @products }
  end
end

def show
  @product = Product.find(params[:id])
  @comment = Comment.new
  @comment.user = current_user
  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @product }
  end
end

Products#show view (working)

    <div class="comment-wrapper">
      <ul class="comments">
          <%= render 'comments/comment' %>
        <li>
          <%= render 'comments/form' %>
        </li>
      </ul>
    </div>

Comments partial _comment.html.erb (working in products#show not working on products#index)

<% @product.comments.each do |comment| %>
 <li>
  <div class="comment-inner-wrapper">
    <div class="comment-controls">
     <% if comment.user == current_user %>          
       <%= link_to [@product, comment], :method => :delete, :confirm => "Are you sure?" do  %>
        <i class="icon-trash"></i>
       <% end %>
     <% end %>
   </div>
   <div class="comment-author-pic">
    <%= link_to comment.user do %>
      <%= image_tag comment.user.image.url(:thumb) %>
    <% end %>
   </div>
   <div class="comment-author">
    <%= link_to comment.user.username, comment.user %>
   </div>
   <div class="comment-content">
     <p><%= comment.content %></p>
   </div>
 </div>
</li>

Products#index view (Im hung up here. I have the form working for each product and it will post a new comment, but only visible if you go the the #show page. Comments wont render on the #index view) I get the error undefined method `comments' for nil:NilClass for line #1 of my comments partial _comment.html.erb

   <div class="comment-wrapper">
      <ul class="comments">
          <%= render 'comments/comment' %>
          <li>
            <div class="comment-box-wrapper">
              <%= form_for [product, Comment.new], :remote => true do |f| %>
                <div class="comment-textarea">
                  <%= f.text_area :content %>
                </div>
                <div class="actions">
                  <%= f.submit "Comment", :class => "btn btn-small pull-right" %>
                </div>
              <% end %>
            </div>
          </li>
      </ul>
    </div>

All my model relationships are correct. Any help would be greatly appreciated!!!

Edit Models below

#product.rb
has_many :comments, dependent: :destroy

#comment.rb
attr_accessible :content, :product_id, :user_id

belongs_to :product
belongs_to :user
4

1 に答える 1

1

I think there is a problem with your models. Following is the sample code for products and comments that allows you to access comments for each product in index page:

#product.rb
class Product < ActiveRecord::Base
  attr_accessible :name
  has_many :comments
end

#comment.rb
class Comment < ActiveRecord::Base
  attr_accessible :product_id, :text
  belongs_to :product
end

#products_controller.rb
class ProductsController < ApplicationController
    def index
        @products = Product.all
    end
end

EDIT If you want to use partial for comments it should be something like this.

#products/index.rb
<% @products.each do |p| %>
    <%= p.name %>
    <%= render 'products/comments', :product => p %>
<% end %>

#_comments.html.erb
<% product.comments.each do |c| %>
    <%= c.text %> 
<% end %>
于 2013-02-23T13:35:48.503 に答える