我正在开发一个 Rails 插件,它需要通过帮助程序输出一个复选框。startup_exchange_optin('user')
将助手放置在脚手架的 Users#index 视图中时,会出现该复选框。将其放置在我得到的新视图、编辑视图和显示视图中NoMethodError
:
undefined method `startup_exchange_optin' for #<User:0x0000010162c620>
文件调用者init.rb
:
# lib/startup_exchange.rb
require 'startup_exchange/startup_exchange_helper'
module StartupExchange
end
ActiveSupport.on_load(:action_view) do
include StartupExchange::StartupExchangeHelper
end
帮手:
# lib/startup_exchange/startup_exchange_helper.rb
module StartupExchange
module StartupExchangeHelper
def startup_exchange_optin(object_name, method = 'startup_exchange_optin', options = {}, checked_value = '1', unchecked_value = '0')
check_box(object_name, 'startup_exchange_optin', options, checked_value, unchecked_value)
end
end
end
该插件不会成为一个 gem,这就是为什么需要init.rb
. 起初我试图使用Railtie
,但我无法让它初始化。ActiveSupport.on_load
似乎至少适用于索引视图。