我正在开发一个Rails插件,它需要通过助手输出一个复选框。将辅助对象startup_exchange_optin('user')
放置在脚手架用户#索引视图中时,会出现此复选框。把它放在新的、编辑和显示的视图中,我得到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
这个插件不会是一块宝石,这就是为什么需要init.rb
。起初,我尝试使用Railtie
,但无法对其进行初始化。ActiveSupport.on_load似乎至少对索引视图有效。
check_box(object_name, 'startup_exchange_optin'...
#=> undefined method `startup_exchange_optin' for #<User:0x0000010162c620>
您的User对象未响应"startup_exchange_optin"方法。