RailsCasts Pro episodes are now free!

Learn more or hide this

Nishank Khanna's Profile

GitHub User: Emprivo

Site: http://www.emprivo.com

Comments by Nishank Khanna

Avatar

If you're using a mountable engine, here's the best way to get the commentable working:

In your routes.rb:

resources :questions do
        resources :comments, :defaults => { :commentable => 'question' }
end

resources :answers do
        resources :comments, :defaults => { :commentable => 'answer' }
end

In your comments_controller.rb:

module MyEngine
  class CommentsController < ApplicationController
          before_action :load_commentable

  private

          def load_commentable
            @commentable = MyEngine::const_get(params[:commentable].classify).find(commentable_id)
          end

          def commentable_id
            params[(params[:commentable].singularize + "_id").to_sym]
          end

  end
end

For mountable engines, you HAVE to do MyEngine:: when constantizing the object. Won't work without it.