【问题标题】:superclass mismatch for class CommentsController (TypeError), best way to rename?类 CommentsController (TypeError) 的超类不匹配,重命名的最佳方法是什么?
【发布时间】:2013-03-31 10:32:15
【问题描述】:

今晚我在部署时遇到了一些问题,我正在尝试尽快解决这个问题

我不知道为什么会这样。在本地一切正常,但在heroku上却不行。在研究之后,我尝试了各种不同的修复方法,但我可能不得不完全重命名这个类 CommentsController(希望这有效)。最好的方法是什么?我对 Rails 很陌生,所以我需要一些帮助来正确更改这些内容。

这是 CommentsController 的外观,仅供参考:

class CommentsController < ApplicationController
  def new
    @post = Post.new(params[:post])
  end

  def show
    @comment = Comment.find(params[:id])
    respond_to do |format|
      format.js
    end
  end

  def create
    @post = Post.find(params[:post_id])
    @comment = Comment.new(params[:comment])
    @comment.micropost = @post
    @comment.user = current_user
    if @comment.save
      redirect_to(:back)
    else
      render partial: 'shared/_comment_form', locals: { post: @post }
    end
  end
end

评论与每个帖子相关联(用户可以对帖子发表评论)。如果需要,我也会发布其他代码。

这是heroku日志中的错误

2013-04-09T05:55:19.454545+00:00 app[web.2]: /app/app/controllers/comments_contr
oller.rb:1:in `<top (required)>': superclass mismatch for class CommentsControll
er (TypeError)

Routes.db

SampleApp::Application.routes.draw do
  resources :posts, :path => "posts"

  resources :users do
    resources :messages do
      collection do
        post :delete_selected
      end
    end
  end

  ActiveAdmin.routes(self)

  devise_for :admin_users, ActiveAdmin::Devise.config

  resources :users do
    member do
      get :following, :followers
    end
  end

  resources :sessions, only: [:new, :create, :destroy]
  resources :posts, only: [:create, :destroy]
  resources :relationships, only: [:create, :destroy]
  resources :posts do
    resources :comments
  end

  root to: 'static_pages#home'

  match '/signup',   to: 'users#new'
  match '/signin',   to: 'sessions#new'
  match '/signout',  to: 'sessions#destroy', via: :delete

  match '/post',    to: 'static_pages#post'
  match '/post1',   to: 'static_pages#post1'
  match '/faq',     to: 'static_pages#faq'
  match '/review',  to: 'users#review'
  match "/posts/:id/review" => "posts#review"
end

当我在 rails app 文件夹中运行高级索引搜索时,这里出现了相关文件

- comments_controller.rb
- comments_helper.rb
- comments_helper_spec.rb
- comments_controller_spec.rb
- 3 migration files
- routes.rb (posted above)
- schema.rb (table called "active_admin_comments" and table called "comments')
- post.rb model (has_many :comments)
- user.rb model (has_many :comments)
- comment.rb model
- active_admin.rb in config/initializer (any instance where I find "comments" has been #'ed out")

【问题讨论】:

  • 您是否在应用程序的任何其他文件中声明了CommentsController
  • 你能用路线更新问题吗?
  • 自上次(工作)部署以来,我在路线中唯一改变的是添加此行匹配 "/posts/:id/review" =&gt; "posts#review"
  • 我什至没有触摸CommentsController 或在任何地方声明它,这就是为什么我不知道该怎么做。我尽力搜索,但没有找到任何地方
  • 你能检查在任何地方找到的 CommentsController 类吗?请使用 grep 命令找到它。例如:grep -n -H -r "评论" ./

标签: ruby-on-rails ruby ruby-on-rails-3 model-view-controller


【解决方案1】:

我遇到了几乎相同的问题(服务器正确启动,但 RSpec 失败并出现同样的错误)。 就我而言,问题出在 ActiveAdmin (0.6.0) 中。不知道具体是什么,可能与命名空间有关。

刚刚降级到 0.5.0 在那个版本上,我对 CommentsController 没有任何问题。

【讨论】:

  • 因为我不需要活动管理员上的 cmets 功能,所以我在 config/initializers/devise.rb 中添加了:“config.allow_cmets = false”,我能够继续使用 ActiveAdmin 0.6.0用我自己的 CommentsController
  • 我认为你的意思是 config/initializers/active_admin.rb 而不是 config/initializers/devise.rb
  • 我仍然在 0.6.0 中遇到这个问题,“config.allow_cmets = false”运行 rspec 测试,尽管它似乎可以通过浏览器工作。降级到 0.5.1 可以解决这个问题。
【解决方案2】:

我假设 ActiveAdmin 有自己的 CommentsController,它来自另一个基类。它只影响运行测试,所以我只是将路由更改为:

unless Rails.env.test?
  devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
end

除非您想针对 ActiveAdmin 中的路由进行测试,否则此解决方案效果很好。

【讨论】:

    【解决方案3】:

    我与 admin 命名空间有类似的冲突,因为我在我的应用程序中定义了 Admin::CommentsController

    尝试将 ActiveAdmin 的默认命名空间更改为“admin”以外的其他名称

    config/initializers/active_admin.rb

    config.default_namespace = :activeadmin # Default :admin
    

    【讨论】:

      【解决方案4】:

      从 Active Admin 0.6.1 开始,您可以重命名 Active Admin 包含的 cmets 模块,这样它就不会与您自己的冲突。这些是选项:

      # == Admin Comments
      #
      # This allows your users to comment on any resource registered with Active Admin.
      #
      # You can completely disable comments:
      # config.allow_comments = false
      #
      # You can disable the menu item for the comments index page:
      # config.show_comments_in_menu = false
      #
      # You can change the name under which comments are registered:
      # config.comments_registration_name = 'AdminComment'
      

      【讨论】:

      • 我在 Gemfile 中添加了“0.6.1”,但它要求我将 jquery-rails 恢复到 3.0 之前的版本
      猜你喜欢
      • 2013-06-18
      • 2018-03-10
      • 2013-12-13
      • 2017-11-03
      • 2016-02-24
      • 2018-04-15
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多