Draiken
Draiken

Reputation: 3815

Rails 3 Engine Problem with Routes

I have an engine, with this routes file:

Rails.application.routes.draw do
  resources :comments, :controller => 'opinio/comments'
end

When I run the rake routes task, I get the correct output

     comments GET    /comments(.:format)           {:action=>"index", :controller=>"opinio/comments"}
              POST   /comments(.:format)           {:action=>"create", :controller=>"opinio/comments"}
  new_comment GET    /comments/new(.:format)       {:action=>"new", :controller=>"opinio/comments"}
 edit_comment GET    /comments/:id/edit(.:format)  {:action=>"edit", :controller=>"opinio/comments"}
      comment GET    /comments/:id(.:format)       {:action=>"show", :controller=>"opinio/comments"}
              PUT    /comments/:id(.:format)       {:action=>"update", :controller=>"opinio/comments"}
              DELETE /comments/:id(.:format)       {:action=>"destroy", :controller=>"opinio/comments"}

My controller is pretty simple:

class Opinio::CommentsController < ApplicationController
  include Opinio::Controllers::InternalHelpers

  def index
    resource.comments.page(params[:page])
  end

  def create
    @comment = resource.comments.build(params[:comment])
    @comment.user = current_user
    if @comment.save
      flash[:notice] = I18n.translate('opinio.comment.sent', :default => "Comment sent successfully.")
    else
      flash[:error]  = I18n.translate('opinio.comment.error', :default => "Error sending the comment.")
    end
  end
end

But when I try using any action that goes to the engine's controller I get the following error:

uninitialized constant Comment::CommentsController

I sincerely don't know where Rails is magically adding this Comment namespace on the controller, and I don't have a clue of how to solve this.

Upvotes: 1

Views: 1017

Answers (1)

Draiken
Draiken

Reputation: 3815

Wow, this deserves an answer so nobody ever do such stupidity like I did.

Basically, I added this to my engine's module:

mattr_accessor :name
@@name = "Comment"

and internally, there is already a method name on every module, which I accidentally overrided, and causing all the errors. AS tried to load the missing constant, but when called for name inside my Opinio model, it got "Comment" instead of Opinio.

A reminder for myself and any others out there. Don`t use obvious names and attributes without checking if they already exist first.

Upvotes: 2

Related Questions