简体   繁体   English

如何在lib文件夹中生成controller / models / views? (Ruby on Rails)

[英]How to generate controller/models/views inside lib folder? (Ruby on Rails)

I'm kind of new to ruby and I encountered this problem. 我是红宝石的新手,遇到了这个问题。

rails generate model whatever creates some files inside the /app/models/ folder. rails generate model whatever/app/models/文件夹中创建一些文件, rails generate model whatever But what if I want to generate those inside lib for example in /lib/modules/monitor/app/ 但是如果我想在例如/lib/modules/monitor/app/

I've been searching and didn't find anything. 我一直在搜索,没有找到任何东西。 I tried putting the path in the command but it doesn't work, it just creates the specified path inside the /app folder. 我尝试将路径放入命令中,但是它不起作用,它只是在/ app文件夹中创建了指定的路径。

Right now what I'm doing is creating a controller for example, and copying the created files from /app to /lib/modules/monitor/app, which is not that good since you have to copy manually all the files. 现在,我正在做的是例如创建一个控制器,然后将已创建的文件从/ app复制到/ lib / modules / monitor / app,但这并不是很好,因为您必须手动复制所有文件。

Is there a better way to do this? 有一个更好的方法吗?

There doesn't seem to be any conventional rails reason for copying files from the app to lib. 从应用程序复制文件到lib似乎没有任何常规的理由。 Will the rails app even be able to use them? Rails应用程序甚至可以使用它们吗? My guess is probably not without breaking a lot of stuff. 我的猜测可能并非没有打破很多东西。 But if you simply want to use rails generators to generate files and have them automatically copy to some other location, you might want to check out this gem 但是,如果您只是想使用Rails生成器来生成文件并将它们自动复制到其他位置,则可能需要查看此gem

gem install listen

You would need to write a ruby script and run it from the root of your project before using your rails generators for example in a file bin/file_duper.rb 在使用rails生成器之前,例如在文件bin/file_duper.rb您需要编写一个ruby脚本并从项目的根目录运行它。

#!/usr/bin/env ruby
require 'listen'
rails_root = Dir.exists?(Dir.pwd + '/app') ? Dir.pwd : nil
abort('This script must be run from rails root') unless rails_root
dest_folder = Dir.pwd + '/lib/modules/'
unless Dir.exists?(dest_folder)
  FileUtils.mkdir(dest_folder)
end

listener = Listen.to(rails_root, dest_folder) do |modified, added, removed|
  added.each do |file|
    FileUtils.cp(file, dest_folder + file.split('app/')[1])
  end
  modified.each do |file|
    FileUtils.cp(file, dest_folder + file.split('app/')[1])
  end
  removed.each do |file|
    FileUtils.rm(file, dest_folder + file.split('app/')[1])
  end
end

listener.start 
sleep

Now make sure you're in the rails project root before running this script. 现在,在运行此脚本之前,请确保您位于rails项目的根目录中。 You might want to run this detached (in the background). 您可能希望以分离的方式运行此程序(在后台)。

ruby bin/file_duper.rb # running in terminal can be killed with ctrl + c

If you want this to run in the background you can run it like 如果您希望它在后台运行,则可以像

ruby bin/file_duper.rb &

This will run several processes in the background under the hood using ruby rb-fsevent. 这将使用ruby rb-fsevent在后台运行多个进程。 If you need to stop these processes you can do 如果您需要停止这些过程,则可以执行

pgrep -f rb-fseven | xargs kill

Words of caution: This script is not heavily tested and may have defects. 警告:该脚本未经严格测试,可能有缺陷。 Also this type of file duplication would likely cause problems with git version control so you may need to add lib/modules/ to your projects .gitignore file assuming you're using version control. 同样,这种类型的文件复制可能会导致git版本控制出现问题,因此,如果您使用的是版本控制,则可能需要将lib/modules/添加到项目.gitignore文件中。

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

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