1

我有一个引擎,带有这个路由文件:

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

当我运行rake routes任务时,我得到了正确的输出

     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"}

我的控制器非常简单:

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

但是,当我尝试使用进入引擎控制器的任何操作时,我收到以下错误:

uninitialized constant Comment::CommentsController

我真的不知道 Rails 是在哪里神奇地Comment在控制器上添加了这个命名空间,我也不知道如何解决这个问题。

4

1 回答 1

2

哇,这值得回答,所以没有人会像我那样愚蠢。

基本上,我将它添加到我的引擎模块中:

mattr_accessor :name
@@name = "Comment"

在内部,name每个模块上都有一个方法,我不小心覆盖了它,并导致了所有错误。AS 尝试加载丢失的常量,但是当name在我的 Opinio 模型中调用时,它得到"Comment"Opinio.

对我自己和任何其他人的提醒。不要在不检查它们是否已经存在的情况下使用明显的名称和属性。

于 2011-04-06T12:48:16.570 回答