Rails 项目中脚本/运行程序的 Ruby 脚本应该放在哪里?
我见过将其放在 lib 文件夹中的示例,以及将其放在 app 文件夹中的另一个示例。 Rails 2.3.8 和 Rails 3 约定是否有标准位置?
I have seen examples where it is put in the lib folder, and another example in the app folder. Is there standard place where it should be put for Rails 2.3.8 and Rails 3 convention?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
脚本必须进入 /scripts 文件夹。当然,如何区分脚本与控制器/模型“需要”的常规 ruby 文件几乎总是令人困惑。如果需要您的脚本来启动/维持您的应用程序,那么是的,它是一个脚本。或者,如果它是一个有时需要的 ruby 文件,或者只是在某些情况下需要它补充模型/控制器,那么您最好将其放在 /lib 文件夹中。
Scripts have to go into the /scripts folder. Of course there's almost always the confusion as to how to differentiate a script from a regular ruby file that is 'required' by a controller/model. If your script is required to start/sustain your application, then yes its a script. Or else if its a ruby file that is need at times or just for some cases wherein it complements the model/controller, you're better off putting it in the /lib folder.
我通常没有运行程序脚本,而是直接调用模型上或 lib 中某些内容中存在的某些方法。例如,我的 Rails cronjobs 通常如下所示:
/path_to_app/scripts/runner -e production "SomeModule.perform_task"
我认为这更干净。
不过,我曾经写过一个脚本,在这种情况下,我只是将其放在 lib 目录中:
/path_to_app/scripts/runner -e production /path_to_app/lib/perform_task.rb"
I usually don't have runner scripts, but instead directly call some method that exists either on a model or in something in lib. For example, my Rails cronjobs typically look like this:
/path_to_app/scripts/runner -e production "SomeModule.perform_task"
I think this is cleaner.
I wrote a script on one one occasion, though, and in that case, I just put it in the lib directory:
/path_to_app/scripts/runner -e production /path_to_app/lib/perform_task.rb"