2012-05-05 71 views
0

因此yesterday我有一個問題,我無法在我的藝術家頁面上顯示來自朋友的評論,我已經設法解決這個問題,但現在我努力在我的圖像上顯示評論。在圖像上顯示評論

我現在想要做的就是顯示評論,我手動輸入到數據庫並顯示在我的圖片下。

這是圖像模式:

class Image < ActiveRecord::Base 
    has_many :publishings 
    has_many :artists, :through => :publishings 
    has_many :comments,:through => :friends 
    has_many :comments, :as => :resource, :class_name => "Commentable" 
end 

這是圖像

<p id="notice"><%= notice %></p> 
<div id="container"> 
    <p> 
    <b>Title:</b> 
    <%= @image.title %> 
    </p> 

    <p> 
    <b>Filename:</b> 
    <%= @image.filename %> 
    <%= image_tag @image.filename %> 
    </p> 

    <p> 
    <b>Likes:</b> 
    <span id="images_<[email protected]%>_likes_count"><%= @image.likes %></span> 
    </p> 
    <div id="comments"> 
    <h2>Comments</h2> 
    <%= render :partial => "shared/comment", :collection => @image.comments%> 
    </div> 
</div> 

<%= link_to 'Like', like_image_path(@image), :method => :post %> | 
<%= link_to 'Edit', edit_image_path(@image) %> | 
<%= link_to 'Back', images_path %> 

的show.html這是我目前收到的錯誤:

> undefined method `first_name' for nil:NilClass 

    Extracted source (around line #16): 

13: </p> 
14: <p> 
15: <b>First name:</b> 
16: <%= @artist.first_name %> 
17: </p> 
18: 
19: <p> 

應用跟蹤

app/views/images/show.html.erb:16:in `_app_views_images_show_html_erb___1549892128_70077336775360_0' 
app/controllers/images_controller.rb:18:in `show' 

這是朋友的模型,它顯示了正確的方法:

class Friend < ActiveRecord::Base 
    has_many:friends 
    has_many :artists, :through => :publishings 

    def display_name 
    "#{self.first_name} #{self.second_name}" 
    end 
end 
+0

你能不能把格式化你的問題更多一些努力呢?另外,'ruby!= ruby​​-on-rails'。你的問題是關於Ruby on Rails的。請適當標記。最後,我想這是一個錯字,所以請從你的「朋友」模型的頂部刪除't_name'作爲nil:NilClass'。 – Mischa

+0

您應該看到一個堆棧跟蹤或其他一些信息,用於標識哪一行代碼正在觸發異常。我在你發佈的代碼中看到的唯一一行是'display_name'的主體,但我看不到任何調用該方法的東西。我想知道這裏是否缺少某些東西,我們需要這些東西來解決這個問題。 –

+0

我的歉意,我上面編輯它顯示應用程序跟蹤。 –

回答

1

在昨天的問題,你發佈你的ImagesControllershow方法不落@artist變量。

此處的堆棧跟蹤表示@artist變量爲零。

但是,因爲你的模型表明,圖像可以有很多的藝術家,你的觀點真的應該在邏輯更改爲:

<% @image.artists.each do |artist| %> 
    <p> 
    <b>First name:</b> 
    <%= artist.first_name %> 
    </p> 
<% end %> 
+0

那我從哪裏出發呢?我真的很陌生,而且我真的很掙扎。 –

+0

它應該是belongs_to:藝術家而不是有很多? 結束 –

+0

勞倫斯。我無法回答,因爲我不知道你的意圖是什麼。 Rails協會代碼讀起來像散文是有原因的。你說這個「形象has_many:藝術家」。這是真的還是不是? –