2016-09-23 2 views
0

Ich versuche herauszufinden, wie polymorphe Kommentare auf Alben- und Fotoseiten angezeigt werden. Wird Beispiel für Albumseite geben.Polymorphe Kommentare funktionieren nicht

Dies ist der NoMethodError erhalte ich, wenn ich das Index-Seite des Albums gehen: enter image description here

Wie kann ich das Problem beheben?

Aktuelle Informationen zur Einrichtung finden Sie weiter unten.

Bitte lassen Sie mich wissen, wenn weitere Informationen benötigt werden. Danke

Aktuelles Schema

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 

Aktuelle Routen

Rails.application.routes.draw do 

    root to: 'albums#index' 

    resources :albums do 
     resources :photos 
    end 

    resources :albums, :photos do 
     resources :comments 
    end 

end 

Kommentare Controller-

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 

Aktuelle Album Index Seite

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

Aktuelle _comment.html.erb in Kommentare Ordner anzeigen

<h1>Comments</h1> 

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

Aktuelle _form.html.erb in Kommentare Ordner anzeigen

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

Antwort

0

Sie verwenden die Einzahl @album in einer Indexansicht, die albums im Plural anzeigt. Wenn Sie die Kommentare für jedes Album anzeigen möchten, müssen Sie @albums durchlaufen.

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

Wenn Sie wollen, dass alle Kommentare, die Alben sind, würden Sie etwas tun:

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

Ihr genau die gleiche Misstake tun, wie oben, wenn es darum geht, in das Formular.

Denken Sie an Ihre Teiltöne als Funktionen, die Argumente nehmen und ein Stück HTML zurückgeben. So starten Sie können von /views/comments/_form.html.erb Refactoring Einheimischen zu verwenden, anstatt von Instanzvariablen:

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

comment ||= commentable.comments.new ist ein raffinierter Trick bedingte Zuweisung, die Sie für neue verwenden das gleiche Formular können/erstellen und bearbeiten/aktualisieren, ohne explizit comment: album.comment.new vorbei.

Sie können dann die Form machen aus innerhalb einer Schleife:

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

Oder von 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

ok, also, statt '<% ​​= render @ album.comments%>' Ich lege deinen ersten Codeblock, dh '<% @ alabels.each do | a | %> ... ', und ich habe diese Nachricht erhalten:' Das erste Argument in der Form kann nicht nil enthalten oder leer sein 'in Bezug auf die erste Zeile '<% = form_for [@commentable, @comment] do | f | %> 'in' _comment.html.form' wie oben beschrieben ... Wie kann ich das beheben? – user273072545345

+0

Sie sollten zunächst über den Unterschied zwischen Instanz und [lokalen Variablen] lernen (http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables). – max

+0

um ... Ich kenne den Unterschied zwischen den beiden ... klar, ich vermisse etwas in meinem Beitrag ... lass es mich wissen? – user273072545345