[英]Override method from gem package in Rails application
I am using the package and initialise it in my rails application in config/initializers/prawn_rails.rb with include PrawnRailsForms
我正在使用package并在我的 rails 应用程序中的 config/initializers/prawn_rails.rb 中使用
include PrawnRailsForms
对其进行初始化
I am trying to override a method to increase the font size of field.upcase with the code below我正在尝试使用以下代码覆盖增加 field.upcase 字体大小的方法
include PrawnRailsForms
DocumentExtensions.module_eval do
def make_field_box(field)
stroke_bounds
bounds.add_left_padding 2
move_down 2
text field.upcase, size: 18
end
end
But the font doesn't get affected in the pdf views even I restarted the server.但是即使我重新启动服务器,字体在 pdf 视图中也不会受到影响。
You need to do the monkey pathing here.你需要在这里做猴子路径。
Create a prawn-rails-forms
directory in lib
directory.在
lib
目录中创建一个prawn-rails-forms
目录。
Create a document_extensions.rb
file inside prawn-rails-forms
directory.在
prawn-rails-forms
目录中创建一个document_extensions.rb
文件。
Add below code to the document_extensions.rb
将以下代码添加到
document_extensions.rb
module PrawnRailsForms module DocumentExtensions private def make_field_box(field) stroke_bounds bounds.add_left_padding 2 move_down 2 text field.upcase, size: 8 end end end
Restart the server.重新启动服务器。
Extended扩展
If you find still not working after restarting the server then如果重新启动服务器后仍然无法正常工作,那么
config.autoload_paths += %W(#{config.root}/lib)
to the config/application.rb
ORconfig.autoload_paths += %W(#{config.root}/lib)
添加到config/application.rb
或prawn-rails-forms
directory to the config/initializers
prawn-rails-forms
目录移动到config/initializers
Please let me know if it works for you.请让我知道它是否适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.