2016-07-14 13 views
0

Ich baue eine App, wo mehrere Objekte Kommentare haben werden. Ich entwarf ursprünglich die App, wo das einzige Ding, das Kommentare haben könnte, Posts waren, aber seitdem Richtungen geändert haben, um Kommentare polyporphic zu machen.Polymorphic Tabelle für Kommentare in Rails

Hier ist mein Post Modell

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 

Kommentare Modell

class Comment < ActiveRecord::Base 
    include Bootsy::Container 

    belongs_to :commentable, polymorphic: true 
    belongs_to :user 
end 

Derzeit Posts (Beiträge in einem Forum) sind Namespace und für meine anderen commentable Objekte, werden sie nicht Namespaces werden. Sollte ich eine CommentsController in den Namespaced forum Controller-Verzeichnis und eine CommentsController in der Haupt-Controller-Verzeichnis haben?

Meine Strecken sehen wie folgt aus (bisher):

# 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 

Antwort

0

Wenn Sie auf jedes Modell verschiedene Ansichten für Kommentare haben wollen dann meiner Meinung nach sollten Sie beide Controller haben.