[英]How to package application layout into a Ruby gem?
我正在一个包含三个不同应用程序的项目。 他们应该共享一些模型和外部布局。 为了避免代码重复,我尝试将应用程序布局( project_name/app/views/layouts/application.html.haml
)提取到gem中。
我按照以下步骤操作:
bundle gem common
创建基础宝石结构 common/app/views/layouts/application.html.haml
写了Gemspec描述符
# -*- encoding: utf-8 -*- require File.expand_path('../lib/common/version', __FILE__) Gem::Specification.new do |gem| gem.authors = ["Arthur Alkmim"] gem.email = ["myemail@here"] gem.description = %q{Common project files} gem.summary = %q{Common project files, including layout and migrations} gem.homepage = "" gem.files = `git ls-files`.split($\\) gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.name = "common" gem.require_paths = ["lib"] gem.version = Common::VERSION end
提交更改
gem build common.gemspec
(成功) rake install
(成功) gem 'common'
放置在项目Gemfile中 但是,该项目仍然不会加载应用程序布局。 我该怎么做才能告诉项目必须通过gem加载布局文件?
提前致谢。
您是否已将宝石添加到Rails Gemfile中以将其包含在应用程序中?
您可以使用path选项指定开发中的相对路径。 例如
gem 'common', :path => "../path/to/gem"
不要忘记然后运行bundle install
我解决了。 我将application.html.haml
更改为common.html.haml
,并将相关的布局调用放置在ApplicationController中。 似乎Rails不允许我将application.html布局打包为gem,但其他布局也可以。 如果有人想出更好的解决方案(较少的解决方法),请发布!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.