2016-09-23 57 views
0

我想弄清楚如何顯示專輯和照片頁面上的多態註釋。下面將給出Album頁面示例。Polymorphic註解不起作用

這是NoMethodError當我去了相冊的索引頁,我收到: enter image description here

如何解決呢?

當前設置信息如下。

如果需要更多信息,請讓我知道。謝謝

當前架構

ActiveRecord::Schema.define(version: 20160916091714) do 

    # These are extensions that must be enabled in order to support this database 
    enable_extension "plpgsql" 

    create_table "albums", force: :cascade do |t| 
    t.string "title" 
    t.string "description" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "comments", force: :cascade do |t| 
    t.text  "content" 
    t.integer "commentable_id" 
    t.string "commentable_type" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 

    create_table "photos", force: :cascade do |t| 
    t.string "name" 
    t.integer "album_id" 
    t.datetime "created_at",   null: false 
    t.datetime "updated_at",   null: false 
    t.string "image_file_name" 
    t.string "image_content_type" 
    t.integer "image_file_size" 
    t.datetime "image_updated_at" 
    t.index ["album_id"], name: "index_photos_on_album_id", using: :btree 
    end 

    add_foreign_key "photos", "albums" 
end 

當前路線

Rails.application.routes.draw do 

    root to: 'albums#index' 

    resources :albums do 
     resources :photos 
    end 

    resources :albums, :photos do 
     resources :comments 
    end 

end 

評論控制器

class CommentsController < ApplicationController 

    def new 
     @comment = @commentable.comments.new 
    end 

    def create 
     @comment = @commentable.comments.new(comment_params) 

     if @comment.save 
      redirect_to :back, notice: 'Your comment was successfully posted!' 
     else 
      redirect_to :back, notice: "Your comment wasn't posted!" 
     end 
    end 

    private 

    def comment_params 
     params.require(:comment).permit(:content) 
    end 

    def find_commentable 
    @commentable = Album.find_by_id(params[:album_id]) if params[:album_id] 
    @commentable = Photo.find_by_id(params[:photo_id]) if params[:photo_id] 
    end 

end 

當前相冊索引頁

<h1>All Albums</h1> 

<%= link_to 'New', new_album_path %> 

<p><%= notice %></p> 

<table> 
    <tr> 
     <th>Title</th> 
     <th>Description</th> 
     <th colspan="2"></th> 
    </tr> 

    <% @albums.each do |p| %> 
    <tr> 
     <td><%= p.title %></td> 
     <td><%= p.description %></td> 
     <td><%= link_to 'Rename Album', edit_album_path(p) %></td> 
     <td><%= link_to 'See Album', album_path(p) %></td> 
     <td><%= link_to 'Destroy', album_path(p), 
       method: :delete, 
       data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
    <% end %> 
</table> 



<h3>Comments</h3> 

<%= render @album.comments %> 

<ul> 
    <%= render 'comments/form' %> 
</ul> 

在評論查看文件夾中當前_comment.html.erb

<h1>Comments</h1> 

<div id="comments"> 
    <% @comments.each do |comment| %> 
    <div class="comment"> 
     <%= comment.content %> 
    </div> 
    <% end %> 
</div> 

目前在評論查看文件夾_form.html.erb

<h1>New Comment</h1> 

<%= form_for [@commentable, @comment] do |f| %> 
    <% if @comment.errors.any? %> 
    <div class="error_messages"> 
     <h2>Please correct the following errors.</h2> 
     <ul> 
     <% @comment.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.text_area :content, rows: 8 %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

回答

0

您正在使用的單數@album在索引視圖中以複數形式顯示albums

如果您想要顯示每個專輯的評論,您需要通過@albums來迭代。

<% @albums.each do |a| %> 
    <%= render a.comments %> 
<% end %> 

如果你想所有的都是你的專輯會做一些這樣的評論:

<aside> 
    <h2>Recent comments</h2> 
    <% Comment.where(commentable_type: 'Album').limit(10).order(created_at: :desc).each |c| %> 
    <%= comment.content %> 
    <% end %> 
</aside> 

你做同樣的misstake當談到上述表單。

把你的部分想象成帶參數並返回大塊html的函數。所以讓我們通過重構/views/comments/_form.html.erb,而不是使用實例變量當地人開始:

<%= form_for([commentable, comment ||= commentable.comments.new) do |f| %> 
    <% # ... %> 
<% end %> 

comment ||= commentable.comments.new是一個極好的技巧使用條件的分配,讓您使用相同的形式爲新/創建並沒有明確地傳遞comment: album.comment.new編輯/更新。

然後,可以從一個循環內呈現的形式爲:

<% @albums.each do |a| %> 
    <h2><%= a.title %></h2> 
    <%= render partial: 'comments/form', commentable: a %> 
<% end %> 

或者從a partial

# albums/index.html.erb 
<%= render @albums %> 

# albums/_album.html.erb 
<article> 
    <h2><%= album.title %></h2> 
    <%= render partial: 'comments/form', commentable: album %> 
</article> 
+0

確定,所以,代替'<(%)=渲染@ album.comments%>' ,我把你的第一個代碼塊,即'<%@ albums.each do | a | %> ...',並且我得到了這樣一條信息:'表單中的第一個參數不能包含nil或者是空的,並且引用第一行'<%= form_for [@commentable,@comment] do | f | %>'在'_comment.html.form'上面貼出來......我該如何解決這個問題? – user273072545345

+0

您應該首先了解實例與[局部變量](http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables)之間的區別。 – max

+0

嗯......我知道兩者之間的區別......顯然我錯過了我的文章中的某些內容......請讓我知道? – user273072545345