2013-02-23 90 views
0

我有一個簡單的應用程序,可以讓用戶上傳產品,然後評論這些產品。在產品上列出評論#index view

我目前有產品#show頁面列出該產品的相關注釋。這部分工作。我似乎無法工作的是,我想在產品的#index頁面上顯示每個產品的評論。

我在想,我爲產品#show view工作的工作也適用於#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動作 「意見」)

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 

產品#放映視圖一個noMethod錯誤(WO rking)

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

評論部分_comment.html.erb(在產品#表現出不工作的產品#指數)工作

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

產品#索引視圖(IM掛了這裏。我有適用於每種產品的表單,它會發布新評論,但僅在#show頁面顯示時纔可見。評論不會呈現在#INDEX視圖)我得到的錯誤未定義的方法`評論的零:NilClass爲我的意見線#1部分_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 

回答

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 %> 
+0

感謝您的回覆!不幸的是,拋出了我以前得到的同樣的錯誤。產品中的NoMethodError#index未定義的方法nil的註釋:NilClass,提取的源代碼(在第16行左右):'<%p.comments.each do | c | %>' – itsthewrongway 2013-02-23 13:53:37

+0

p.s.我已經在編輯中發佈了我的模型。他們已經像你所建議的那樣。 – itsthewrongway 2013-02-23 14:01:54

+0

這可能意味着您嘗試訪問的產品實例的註釋不存在。我認爲每個產品評論的部分內容都試圖訪問索引控制器操作未提供的@product。您需要通過索引視圖將產品傳遞給部分。 – 2013-02-23 14:03:54