簡體   English   中英

在Products#index視圖上列出評論

[英]Listing comments on the Products#index view

我有一個簡單的應用程序,可讓用戶上傳產品,然后對這些產品發表評論。

我目前在產品#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

產品#顯示視圖 (工作中)

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

注釋_comment.html.erb部分 (在products#show中無法使用在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視圖 (我在這里掛了。我有適用於每種產品的表單,它會發布新的注釋,但是只有當您進入#show頁面時才可見。注釋不會在#index視圖上呈現)我的注釋的第1行的nil:NilClass的錯誤未定義方法'comments'部分_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

我認為您的模型有問題。 以下是產品和注釋的示例代碼,使您可以在索引頁面中訪問每個產品的注釋:

#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

編輯如果您想使用partial進行評論,它應該是這樣的。

#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 %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM