مشكلة مع جمعية متعددة الأعمدة في القضبان
-
18-09-2019 - |
سؤال
أحاول اتباع ريان بيتس 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) بعد ذلك أحصل على رسالة خطأ عن طريق الوصول / الفئات / بلدي الفئة الرابط الثابت
NoMethodError in Categories#show
undefined method `category_comments_path' for #<ActionView::Base:0x69a9254>
هل يمكن أن تساعدني في فهم ما فعلته خطأ؟ في ScreenCast الأصلي Ryan يصل إلى تعليقات / فئات / Permalink / تعليقات باستخدام الجمعيات المتداخلة، لكنني لا أحتاج ذلك. أريد أن أكتب تعليقات مباشرة من كائنات متعددة الجنسية. شكرا
المحلول
كانت المشكلة في إعدادات الطرق. اعتقدت أنه نظرا لأنني لا أستخدم موارد متداخلة، يمكنني الاحتفاظ بالطرق دون تغيير. حسنا، الآن أعرف أنني كنت مخطئا ... :) أضف هذا لإصلاح المشكلة:
map.resources :categories :has_many => :comments
map.resources :products, :has_many => :comments
لا تنتمي إلى StackOverflow