Rails 中的多态关联问题

发布于 2024-08-06 02:44:02 字数 1684 浏览 6 评论 0原文

我正在尝试关注 Ryan Bates 截屏视频,但收到错误消息。我做了以下操作:

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)将表单添加到模板views/categories/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-收到错误消息 ?

NoMethodError in Categories#show
undefined method `category_comments_path' for #<ActionView::Base:0x69a9254>

你能帮我理解我做错了什么吗 在原始截屏视频中,Ryan 使用嵌套关联访问 /categories/permalink/comments 的评论,但我不需要。我想直接从我的多态对象编写注释。 谢谢

I am trying to follow Ryan Bates screencast but have an error message. I did the following:

1) Create table

class CreateComments < ActiveRecord::Migration
  def self.up
    create_table :comments do |t|
      t.references :commentable, :polymorphic => true

2) Setup models

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) Change controller show action

class CategoriesController < ApplicationController
  def show
    @category = Category.find_by_permalink(params[:id])
    @commentable = @category
    @comment = Comment.new(:commentable => @category)
  end

4) Add a form to template views/categories/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) After that I get error message by accessing /categories/my-category-permalink

NoMethodError in Categories#show
undefined method `category_comments_path' for #<ActionView::Base:0x69a9254>

Could you help me to understand what I did wrong?
In the original screencast Ryan accesses comments by /categories/permalink/comments using nested associations, but I don't need that. I want to write comments directly from my polymorphic objects.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ懵逼小萝莉 2024-08-13 02:44:02

问题出在路线设置上。我认为由于我不使用嵌套资源,因此我可以保持路由不变。好吧,现在我知道我错了......:)添加这个来解决问题:

map.resources :categories :has_many => :comments
map.resources :products, :has_many => :comments

The problem was in routes settings. I thought that since I don't use nested resources, I can keep routes unchanged. Well, now I know that I was wrong... :) Add this to fix the problem:

map.resources :categories :has_many => :comments
map.resources :products, :has_many => :comments
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文