Search code examples
ruby-on-railssimple-form

How to use helper method with input_html with simple_form


I want to change the id of a simple_form input select element using a helper method, i.e.

<%= f.input :no_players, collection: (0..4), input_html: { id_for_trigger } %>

where the helper method is

def id_for_trigger
    return unless @select_trigger
    'id: :no_players_trigger'
end

but this gives following error message

syntax error, unexpected '}', expecting =>
input_html: { id_for_trigger }  );

How do I fix this?


Solution

  • The curly braces signify that you need to pass in a hash. Try this...

    input_html: { id: id_for_trigger }
    

    :input_html sets a html attribute so you need to specify what attribute you are trying to set.

    So, if this is the case, your helper should be....

    def id_for_trigger
      return 'your_default_id' unless @select_trigger
      'no_players_trigger'
    end
    

    The your_default_id is the id that the select element normally has. In this way, any other pages that use css or javascript attached to your_default_id will not be affected.