occasionally useful ruby, ubuntu, etc

7May/082

select_control in merb_helpers

So I'm trying to switch from Ruby on Rails to Merb and...naturally having some difficulties. Now I love the promise of merb, but having to hunt down plugins and documentation is, frankly, a huge bitch. Anyway, I'll try to share pointers in the future for stuff I come across.

Now, on to the heart of the matter. In Rails, when specifying a collection for a select_field, you can specify the collection manually by specifying a array in the format [["key1", "value1"], ["key2", ["value2"]] sort of fashion. However, this won't work for merb -- it expects an array ['key1, 'key2'] that is used for keys and values. But, merb has a couple other properties for select_control, namely :text_method and :value_method. If you pass :first in for :text_method and :last in for :value_method, you'll get the same behavior as in Rails! In other words...

<%= select_control :joinable, :label => "Joinable?", :collection => [['hi', '3']], :text_method => :first, :value_method => :last %>

will display a select field with the text "hi" with the value "3". Enjoy!

Filed under: merb 2 Comments