插件视图助手在新视图,编辑视图和显示视图上失败

时间:2013-01-15 16:31:03

标签: ruby-on-rails ruby-on-rails-3

我正在开发一个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

该插件不会成为一个宝石,这就是init.rb的需要。起初我尝试使用Railtie但我无法初始化它。 ActiveSupport.on_load 似乎至少适用于索引视图。

0 个答案:

没有答案