views:

41

answers:

2

So I have this code inside my lib/ folder:

class GlobalConfig::SetHelper
  def self.yes_no_input(configuration)
    value = configuration.value
    name = configuration.name
    "#{radio_button_tag name, true, (value == true), {:id => "#{name}_yes"}} #{label_tag "#{name}_yes", 'yes'}
     #{radio_button_tag name, false, (value.blank? or value == false), {:id => "#{name}_no"}} #{label_tag "#{name}_no", 'no'}"
  end
end

But it returned: undefined method `radio_button_tag' for GlobalConfig::SetHelper:Class when I run the code.

How do I fix this. Anyone?

Thanks

+3  A: 
  1. Why do you put this into lib? app/helpers is the standard place to put helper methods like this.
  2. Having helper methods as class methods of the helper class is not the way to go. You should rather make it an instance method. Then put this code into your controller:

    helper GlobalConfig::SetHelper

    and you'll have access to its methods inside your views. And standard helper methods like radio_button_tag will also work at once.

neutrino
I am aware it would be a piece of cake to do it like so, and this is what I am actually doing now until I find a solution.Basically my site will have various configuration settings I defined in database (so non-programmer site admin can easily change them). Some configs are not straightforward for example countries will need a country selector, but for most other things can do with text inputs.To easily allow other programmers (or myself) adding new input styles, I encapsulate the functions into lib folder config/set_helpers and config/get_helpers.So is this possible?
jaycode
A: 

You need include in your helper the Helper with this method :


class GlobalConfig::SetHelper
  include ActionView::Helpers::FormTagHelper
end
shingara
I think I have tried that before, but I will try again, maybe I wrote it wrong. I'll let you know!
jaycode
The code gets too big I haven't had time to check this, if anyone could verify would be appreciated.
jaycode
worked, thanks!
jaycode