我想弄清楚如何显示专辑和照片页面上的多态注释。下面将给出Album页面示例。Polymorphic注解不起作用
这是NoMethodError当我去了相册的索引页,我收到:
如何解决呢?
当前设置信息如下。
如果需要更多信息,请让我知道。谢谢
当前架构
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 %>
确定,所以,代替'<(%)=渲染@ album.comments%>' ,我把你的第一个代码块,即'<%@ albums.each do | a | %> ...',并且我得到了这样一条信息:'表单中的第一个参数不能包含nil或者是空的,并且引用第一行'<%= form_for [@commentable,@comment] do | f | %>'在'_comment.html.form'上面贴出来......我该如何解决这个问题? – user273072545345
您应该首先了解实例与[局部变量](http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables)之间的区别。 – max
嗯......我知道两者之间的区别......显然我错过了我的文章中的某些内容......请让我知道? – user273072545345