3

/lib にモジュールがあります

Module Info
class Inf

  def getNum
    num = Array.new

    num.push(2,1)

  end

end

コントローラーの informations_controller には、「require Info」と次のコードがあります。

  def index
    @informations = Info::Inf.getNum().num

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @informations }
    end
  end

しかし、それは常にエラーを与えます

Routing Error

uninitialized constant Info

ルーターで "root :to => 'informations#index'" を定義したので、何が不足している可能性がありますか?

4

1 に答える 1

4

moduleモジュールではなく、ファイルに名前を付ける必要info.rbがあります。また、libがauto_loadパスにあることを確認する必要がありますconfig/application.rb

config.autoload_paths += %W(#{config.root}/lib)

したがって、次のようになりますlib/info.rb

module Info
  class Inf
    ...
  end
end
于 2012-05-07T14:37:05.973 に答える