Rails 4.1 在自动加载中包含哪些文件夹 [英] What folders does Rails 4.1 include in autoload

查看:33
本文介绍了Rails 4.1 在自动加载中包含哪些文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ruby 2.11 编写 Ruby on Rails 4.1 应用程序.结构是域-> 团队-> 用户.因此,用户属于一个团队,我有时需要从域中创建一个团队(因此它们是关联的).想想星巴克 -> 纽约中央团队 -> 咖啡师先生.

I am writing a Ruby on Rails 4.1 app with Ruby 2.11. The structure is Domain -> Team -> User. So, a user belongs to a team and I sometimes need to make a team from a domain (so they are associated). Think Starbucks -> NY Central Team -> Mr Barista.

我制作了一些构建器类并将它们放在 app/builders 中,但是当我尝试使用该类时,它在 Rails 控制台中显示未初始化的常量.因此,例如,我在 app/builders/team_builder.rb 中有该文件:

I have made some builder classes and put them in app/builders, but when I try to use the class it says uninitialized constant in the Rails console. So, for example, I have the file in app/builders/team_builder.rb:

class TeamBuilder

  attr_reader :domain, :params

  def initialize(domain, params = {})
    @domain = domain
    @params = params
  end

  def build
    domain.teams.build(params)
  end

end

但是当我输入 TeamBuilder.new(domain, name: 'Team name here!') 我得到响应

But when I type TeamBuilder.new(domain, name: 'Team name here!') I get the response

NameError: uninitialized constant TeamBuilder

它似乎无法识别我上面添加的新类,这让我认为它没有加载它.但是我以为app/中的所有子目录都加载了.

It seems like it does not recognise the new class I have added above, which makes me think it is not loading it. But I thought that all sub-directories in app/ were loaded.

完全被这个难住了,我找不到这方面的指南或文档(也许它在那里 - 某处......)

Totally stumped on this one, and I cannot find a guide or documentation on this (maybe it's there - somewhere...)

推荐答案

进入 Rails 控制台 (rails c) 并输入以下内容:

Go into your rails console (rails c) and type the following:

YourAppName::Application.config.eager_load_paths

(其中 YourAppName 是在您的 environment.rb 中调用 initialize! 的任何名称).这应该会显示所有自动加载到应用程序中的路径(在自定义之前).

(where YourAppName is whatever it is called in your environment.rb where initialize! is called). This should show you all the paths automatically loaded into your application (before customizing).

看起来这里正在发生自动路径添加魔法https://github.com/rails/rails/blob/master/railties/lib/rails/engine/configuration.rb

It looks like the automatic path adding magic is happening in here https://github.com/rails/rails/blob/master/railties/lib/rails/engine/configuration.rb

这篇关于Rails 4.1 在自动加载中包含哪些文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆