多态注释不起作用

时间:2016-09-23 07:45:29

标签: ruby-on-rails

我试图找出如何在相册和照片页面上显示多态注释。将在下面给出专辑页面示例。

这是我进入相册的索引页面时收到的 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 %>

1 个答案:

答案 0 :(得分: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>

你在表格上做的事与上面完全相同。

将您的partials视为带参数并返回一大块html的函数。因此,让我们从重构/views/comments/_form.html.erb开始,使用locals而不是实例变量:

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

comment ||= commentable.comments.new是一个使用条件赋值的漂亮技巧,它允许您在不明确传递comment: album.comment.new的情况下使用相同的表单进行new / create和edit / update。

然后,您可以在循环中呈现表单:

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