0
我正在構建一個應用程序,其中多個對象將有評論。我最初設計的應用程序中唯一可以評論的是帖子,但是因爲已經改變了方向以使評論變得多態。Rails中的評論多態表
這裏是我的Post
模型
class Post < ActiveRecord::Base
include Bootsy::Container
belongs_to :user
belongs_to :post_category
has_many :comments, as: :commentable
validates_presence_of :post_category, :user
scope :sticky, -> { where sticky: true }
scope :not_sticky, -> { where sticky: false }
scope :for_category, ->(cat_id) { where post_category_id: cat_id }
def is_new?
created_at > Time.now - 24.hours
end
end
評論型號
class Comment < ActiveRecord::Base
include Bootsy::Container
belongs_to :commentable, polymorphic: true
belongs_to :user
end
目前,Posts
(在一個論壇的帖子)的命名空間和我的其他commentable
對象,他們不會被命名空間。我應該在主控制器目錄中的命名空間forum
控制器目錄和 a CommentsController
中有一個CommentsController
嗎?
我的路線是這樣的(到目前爲止):
# Recently added
resources :comments, only: [:create]
namespace :forum do
resources :posts, only: [:index] do
resources :comments, only: [:create]
end
resources :post_categories, only: [:index] do
resources :posts, only: [:index, :show, :new, :create, :edit, :update]
end
end