Rails4,加载ActiveRecord模型无法正常工作

时间:2014-12-08 02:13:27

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

我的routes.rb文件是

 namespace(:api){
  resources :open_conversations do
    resources :open_answers, :only => [:index, :create, :destroy]
 end

}

和OpenConversation和OpenAnswer控制器文件

class OpenConversation < ActiveRecord::Base
  has_many :open_answers, :dependent => :destroy
end


class OpenAnswer < ActiveRecord::Base
  belongs_to :open_conversation
end

我将OpenConversation模型加载到OpenAnswer控制器

class Api::OpenAnswersController < Api::ApplicationController

  before_filter :get_conversation

  def get_conversation
    binding.pry
    @con = OpenConversation.find(params[:open_conversation_id])
    @answers = @con.open_answers
  end
end

============== 这是IRB调试日志

[6] pry(#)&gt; OpenConversation

=&GT;的阿比:: OpenConversation

[7] pry(#)&gt; OpenConversation.find(PARAMS [:open_conversation_id])

NameError:未初始化的常量Api :: OpenAnswersController :: OpenConversation

from(pry):6:在`get_conversation'

[8] pry(#)&gt; OpenConversation

NameError:未初始化的常量Api :: OpenAnswersController :: OpenConversation

from(pry):7:在`get_conversation'

[9] pry(#)&gt; OpenConversation.class

NameError:未初始化的常量Api :: OpenAnswersController :: OpenConversation

from(pry):8:在`get_conversation'

为什么OpenConversation模型加载喜欢模块?

1 个答案:

答案 0 :(得分:0)

你可以给控制器文件树结构。可能是ApplicationController错误地继承了。

class Api::OpenAnswersController < ApplicationController