简体   繁体   English

Rails 3类可供查看,控制器和模型

[英]Rails 3 class available to view, controller and model

I am thinking about creating a new class that does URL shortening that can be called from the view, controller or model. 我正在考虑创建一个可以缩短URL的新类,可以从视图,控制器或模型中调用它。 Where should I put this class? 我应该在哪里上这节课?

这样的库应该位于lib ,您需要在使用它的文件中对其进行要求,或者在config/application.rb globaly中对其进行要求。

您始终可以将其转储到config/initializers除非您要将其打包为独立的gem。

If you place the class in /lib don't forget to make sure that it is autoloaded. 如果将类放在/ lib中,请不要忘记确保已自动加载该类。 In application.rb make sure you have a line like the following: 在application.rb中,确保您有如下一行:

    # Custom directories with classes and modules you want to be autoloadable.
    config.autoload_paths += Dir["#{config.root}/lib/**/"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM