Select Tag Funkiness With Merb

In my ongoing travails with Merb & Friends, I’ve run across a few behaviors with the select() form helper method. Since there’s currently a dearth of examples, I thought I’d document them for myself and future googlers.

If you’re having problems getting select() to work in Merb, bear the following in mind:

If you type the following:

1select :widget_id, :collection => Widget.all

You may get the error:

undefined method 'first' in <Widget:0x7fae15562fc8>

(you may also get “undefined method ‘last’”)

select() expects two options to be passed in: :value_method and :text_method. If you provide neither, Merb will assume that your collection is an array, and will default them to the ‘first’ and ‘last’ methods respectively. Add your field names or methods explicitly to make select() happy:

1
2select :widget_id, :collection => Widget.all, :value_method => :id,
       :text_method => :name

Additionally, there’s a strange behavior, in which the :selected option appears to be ignored– when you pull your form back up after a save, the select box will be back on the default value:

1
2select :widget_id, :collection => Widget.all,:value_method => :id,
       :text_method => :name, :selected => @whongo.widget_id

The problem here is that Merb expects the :selected option to be a string. Cast your selected value to a string and all will be well:

1
2select :widget_id, :collection => Widget.all,:value_method => :id,
       :text_method => :name, :selected => @whongo.widget_id.to_s

Comments

Claus said on Friday, January 23, 2009:

Woohoo! You made my day! I was tearing my hair out esp. figuring out the “:selected” problem.

Thanks a lot for the very helpful post. the merb-manual should have a link to this entry ;-)

Calamitous said on Sunday, January 25, 2009:

Awesome! Glad it helped :)

johan said on Wednesday, February 04, 2009:

I’ve tried the above using Haml (Merb/Datamapper), but without much success.

Eg.

%select {:method, :collection => Method.all, :valuemethod => :name, :textmethod => :name}

I’m basically selecting the label itself (name), for example “GET”, “PUT” as opposed to grabbing the record ID.

Can that cause any problems?

This is the resultant html ():

Seems as if the helper is not even trying to interpret the string?

Calamitous said on Monday, February 09, 2009:

@johan: Are you using Haml? If so, your code is being interpreted as a direct tag translation, since it’s prepended with a ‘%’. It’s probably never getting past Haml.

To use the select helper, start your line with ‘=’, this will pass your request to the interpreter to be rendered.

Raj said on Friday, February 13, 2009:

Guys,

It’s not working properly in Ruby 1.8.6(in production server) and its not showing the selected value in that.

Its working fine Ruby 1.8.7(my local machine) and it is showing selected value also…

can u help me guys….in this issue

cool said on Friday, February 13, 2009:

It’s not working properly in Ruby 1.8.6(in production server) and its not showing the selected value in that.

Its working fine Ruby 1.8.7(my local machine) and it is showing selected value also…

can u help me guys….in this issue

I need to solve this issue urgently….

Post a comment


(required, but not displayed)

(optional)