I have a select dropdown where I generate the options from database entries, then add an option to the beginning like:
@select = Service.find_services_by_id(id).collect { |p| [p.name, p.id] }
@select.unshift( [ "Choose a service", 0] )
Then in the HAML view I have:
=select_tag "service_id", options_for_select(@select)
But I’m looking at refactoring that either by:
1) Adding another method to the model which also does the unshift operation to return to me all the data for the select in one piece
2) Moving the whole @select definition to a view helper and calling it from the view
3) Just have that first @select line in the controller, then have a view helper do the ‘unshift’ part
But I’m having trouble figuring out what makes the most sense. Thoughts?
I’d go for 3rd, but not only do the
unshiftin the helper, but create wholeselectthingie there too:Then in view you’d just call
=service_select(@select).