I am building out an app where multiple objects will have comments. I originally designed the app where the only thing that could have comments were Posts, but since have changed directions to make comments polymporphic.
Here is my Post
model
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
Comments Model
class Comment < ActiveRecord::Base
include Bootsy::Container
belongs_to :commentable, polymorphic: true
belongs_to :user
end
Currently, Posts
(posts in a forum) are namespaced and for my other commentable
objects, they won't be namespaced. Should I have a CommentsController
in the namespaced forum
controllers directory and a CommentsController
in the main controllers directory?
My routes look like this (so far):
# 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
如果您希望对每个模型有单独的意见视图,那么我认为您应该同时拥有两个控制器。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.