【发布时间】:2009-09-26 22:24:46
【问题描述】:
我正在尝试关注Ryan Bates screencast,但收到一条错误消息。我做了以下事情:
1) 创建表
class CreateComments < ActiveRecord::Migration
def self.up
create_table :comments do |t|
t.references :commentable, :polymorphic => true
2) 设置模型
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
has_many :comments, :as => :commentable
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
has_many :comments, :as => :commentable
3) 更改控制器显示操作
class CategoriesController < ApplicationController
def show
@category = Category.find_by_permalink(params[:id])
@commentable = @category
@comment = Comment.new(:commentable => @category)
end
4) 将表单添加到模板视图/类别/show.html.erb
<% form_for [@commentable, Comment.new] do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :content %><br />
<%= f.text_area :content %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
5) 之后我通过访问 /categories/my-category-permalink 收到错误消息
NoMethodError in Categories#show
undefined method `category_comments_path' for #<ActionView::Base:0x69a9254>
你能帮我理解我做错了什么吗? 在原始截屏视频中,Ryan 使用嵌套关联通过 /categories/permalink/cmets 访问 cmets,但我不需要。我想直接从我的多态对象编写 cmets。 谢谢
【问题讨论】:
-
在截屏页面上,Val 发布了他更正的演示代码的链接: 59. Val 2009 年 8 月 25 日 10:02 我修复了您可以从 www.rubyf 下载的所有错误和工作代码。信息/文件/polimorphic_work0.zip
标签: ruby-on-rails polymorphic-associations