0

我有一个简单的应用程序,可以让用户上传产品然后评论这些产品。

我目前的产品#show 页面列出了该产品的相关评论。所以那部分有效。我似乎无法工作的是,我想在产品#index 页面上显示每个产品的评论。

我在想我为 products#show 视图工作的内容也适用于 #index 视图,但在我的情况下似乎并非如此。

评论控制器

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

产品控制器(我不确定我应该在此处的索引操作中添加什么。我尝试过的没有任何效果。@comments = @product.comments 在 productController#index 操作的“comments”上引发 noMethod 错误)

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

产品#show view (working)

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

部分评论 _comment.html.erb(在产品中工作#show 不适用于产品#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 视图(我挂在这里。我有适用于每个产品的表单,它会发布一条新评论,但只有在您进入#show 页面时才可见。评论不会在#index 视图上呈现)我明白了错误未定义方法 `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>

我所有的模型关系都是正确的。任何帮助将不胜感激!!!

编辑下面的模型

#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

我认为你的模型有问题。以下是产品和评论的示例代码,可让您访问索引页面中每个产品的评论:

#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

编辑如果你想使用部分评论它应该是这样的。

#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 回答