简体   繁体   English

Rails bootstrap gem Monkeypatching方法不起作用

[英]Rails bootstrap gem monkeypatching method not working

I'm using the excellent twitter-bootstrap-rails gem. 我正在使用出色的twitter-bootstrap-rails gem。 There is a helper within that gem ( NavbarHelper ) which is used to generate Bootstrap navbars with a Ruby helper. 在该gem中有一个帮助器( NavbarHelper ),用于使用Ruby帮助器生成Bootstrap导航栏。 I want to monkey patch the gem such that the dropdown lists won't have carets. 我想用猴子修补宝石,以使下拉列表中没有插入符号。

So, I looked into the source and found the relevant method here . 因此,我调查了源代码,并在此处找到了相关的方法。 All I have to do is override it. 我要做的就是覆盖它。 I created a new file in config/initializers called navbar.rb with the following content: 我在config / initializers中创建了一个名为navbar.rb的新文件,其内容如下:

NavbarHelper.module_eval do
  def name_and_caret(name)
    "HELLO WORLD"
  end
end

Presumably, all of the dropdown titles then should be rendered as "HELLO WORLD" in my app (as referenced by the gem source). 大概所有下拉列表标题都应在我的应用程序中呈现为“ HELLO WORLD”(由gem来源引用)。 However, this is not occurring, and the gem does not appear to be monkeypatched at all. 但是,这没有发生,并且宝石似乎根本没有被猴子修补。

I tried placing puts NavbarHelper.methods - Object.methods in the initializers file, and there were no results, which makes me think that Rails is not loading the gem correctly before the initializers. 我尝试将puts NavbarHelper.methods - Object.methods放置在初始化文件中,但没有结果,这使我认为Rails不能在初始化之前正确加载gem。 I have also checked and verified that the gem is not using autoload for its helpers. 我还检查并验证了gem没有为其助手使用自动加载功能。

Edit 编辑

What may be complicating this is the fact that my Gemfile includes the gem in the following manner: 这可能使我的Gemfile以以下方式包含gem的事实变得复杂:

gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git', branch: 'bootstrap3'

I'm not sure if this specific versioning means the monkeypatching doesn't work. 我不确定这个特定的版本是否意味着Monkeypatching不起作用。

Edit #2 编辑#2

It seems there is only one version of the gem on my system, so I don't think that's the issue. 看来我的系统上只有gem的一个版本,所以我不认为这是问题所在。 Also, I have tried placing require 'twitter-bootstrap-rails at the top of the initializers file, with no results. 另外,我尝试将require 'twitter-bootstrap-rails放置在初始化文件的顶部,但没有结果。

The problem is that you patch the method on this module but the module already got included at this point. 问题是您在此模块上修补了方法,但此时模块已包含在内。 Try to define this in your application_helper.rb 尝试在application_helper.rb中定义它

def name_and_caret(name)
  super("blub #{name}")
end

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

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