【发布时间】:2015-07-08 03:39:44
【问题描述】:
我有一个安装了Rails engine 的 Rails 应用程序。我在引擎中创建了一个Rails generator。从 Rails 应用程序调用生成器。生成器的目的是从 Rails 引擎复制一组视图,并将它们添加到 Rails 应用程序的视图文件夹中。
Rails 生成器工作正常,但我需要使用 Ruby class Dir glob method 重构它。
这是我的views_generator.rb 文件,它运行良好:
class Speaker::ViewsGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
def generate_participant_views
copy_file "#{copy_path}/participants/_form.html.erb", "#{paste_path}/participants/_form.html.erb"
copy_file "#{copy_path}/participants/edit.html.erb", "#{paste_path}/participants/edit.html.erb"
copy_file "#{copy_path}/participants/index.html.erb", "#{paste_path}/participants/index.html.erb"
copy_file "#{copy_path}/participants/new.html.erb", "#{paste_path}/participants/new.html.erb"
copy_file "#{copy_path}/participants/show.html.erb", "#{paste_path}/participants/show.html.erb"
end
def generate_room_views
copy_file "#{copy_path}/rooms/_form.html.erb", "#{paste_path}/rooms/_form.html.erb"
copy_file "#{copy_path}/rooms/edit.html.erb", "#{paste_path}/rooms/edit.html.erb"
copy_file "#{copy_path}/rooms/index.html.erb", "#{paste_path}/rooms/index.html.erb"
copy_file "#{copy_path}/rooms/new.html.erb", "#{paste_path}/rooms/new.html.erb"
copy_file "#{copy_path}/rooms/show.html.erb", "#{paste_path}/rooms/show.html.erb"
end
private
def copy_path
'../../../../../app/views/speaker'
end
def paste_path
'app/views/speaker'
end
end
我想使用 glob 方法遍历给定文件夹中的所有文件,而不是写出每个文件。但为了做到这一点,我首先需要 cd 进入 Rails 引擎的 views 文件夹。
所以这是我的问题。如何从我的 Rails 应用程序进入已挂载的 Rails 引擎内的目录?
我想将上面的generate_participant_views 方法替换成这样的:
def generate_participant_views
Dir.chdir "#{copy_path}/participants"
all_files = Dir.blob('*')
all_files.each do |file|
copy_file file, paste_path + file
end
end
显然该方法的第一行不起作用,因为它是从 Rails app 内部调用的,而不是引擎。
那么如何从应用程序的有利位置将目录更改为已安装的 Rails 引擎内的文件夹?
谢谢!
【问题讨论】:
-
不幸的是,这将我带到了 Rails 应用程序的根目录,但仍然没有授予我对已安装 Rails 引擎的任何访问权限。谢谢@mudasobwa
标签: ruby-on-rails ruby rails-engines