【问题标题】:Rails 3 Engine Problem with Routes路线的Rails 3引擎问题
【发布时间】:2011-04-06 11:24:55
【问题描述】:

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

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 命名空间,我也不知道如何解决这个问题。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 routes rails-engines


    【解决方案1】:

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

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

    mattr_accessor :name
    @@name = "Comment"
    

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

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

    【讨论】:

      猜你喜欢
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      相关资源
      最近更新 更多