select_control and select_field error when using :collection => %w(one two three)
Reported by Richard Grundy (RichGuk) | August 15th, 2008 @ 04:01 PM | in 0.9.5
<%= select_control :foo, :collection => %w(one two three) %>
Will throw: undefined method 'foo' for one:String
I think the problem might be to do with this method (around line 122 of merb_helpers-0.9.5/lib/merb_helpers/form/builder.rb):
def update_select_control_field(method, attrs)
attrs[:value_method] ||= method
attrs[:text_method] ||= attrs[:value_method] || :to_s
attrs[:selected] ||= @obj.send(attrs[:value_method])
end
So I think it's attempting to give a value_method, text_method of the first param passed into select_control (:foo).
Quick work around I've found this works:
<%= select_control :foo, :collection => %w(one two three), :value_method => :to_s, :text_method => :to_s %>
Comments and changes to this ticket
-

thefool808 August 15th, 2008 @ 10:32 PM
This diff may help... I did need to change one spec which people might not like, but I think it makes more sense.
-
Michael Klishin (antares) August 16th, 2008 @ 01:23 AM
- → Assigned user changed from to GMFlash
- → Milestone changed from to 0.9.5
- → State changed from new to open
I applied and pushed your patch, it may still need a review though.
-

Richard Grundy (RichGuk) August 16th, 2008 @ 02:31 PM
- no changes were found...
-

Richard Grundy (RichGuk) August 16th, 2008 @ 02:31 PM
- → Assigned user cleared.
There was still a problem where the select tag would be given a value key (
Also each option tag would be given the id and name keys from the select tag.
I've attached a patch in an attempt to fix this. I've also changed the order the prompt and blank option tags get added, they were before being added after the collection option tags so the prompt would be at the end - which didn't seem right?
-

Richard Grundy (RichGuk) August 16th, 2008 @ 02:39 PM
- → Assigned user changed from to GMFlash
-

thefool808 August 16th, 2008 @ 10:02 PM
- no changes were found...
-

thefool808 August 16th, 2008 @ 10:02 PM
I've added a new patch. (It includes Richard's patch as well.) I've removed the setting of the :value_method and :text_method attrs from various places and just leaves it in the options method, which is where it's used anyways.
-
Bryan Ray August 16th, 2008 @ 10:18 PM
This patch is kind of in relation with this ticket, but it adds the ability to do:
select_control( :foo, :collection => [['0', 'FakeModel']] )
-
Bryan Ray August 17th, 2008 @ 01:00 AM
+1 for thefool808's patch.
I merge it in to my local repo and added my spec that proves you can pass an array of array's to the collection.
Very nice. Thanks, thefool808
-

GMFlash August 19th, 2008 @ 11:44 AM
<%= select :foo, :collection => %w(one two three).collect { |i| [i, i.capitalize] } %>solves Richard Grundy's original issue in the ticket. I will talk to wycats about patching the helper to perform the above .collect if you supply a non-nested Array to :collection.
-
Bryan Ray August 20th, 2008 @ 09:09 AM
- no changes were found...
-
Bryan Ray August 20th, 2008 @ 09:09 AM
A little patch the kind of wraps up GMFlash's :collection hack above.
I'm sure this needs to be extended and thought out some more as I'm sure that GMFlash and wycats will do ... but I think this might be a start.
-

thefool808 August 20th, 2008 @ 11:57 PM
My patch @ merb_form_helpers.patch fixed the original problem sent by Richard Gundy. It also maintains the original behavior of using first and last on nested arrays. It finally DRYs up the multiple places around the form/builder.rb that the text_method and value_method attrs were referred to.
Unrelatedly, it also allows select_control to be called without error when there is no form_for(@obj). This functionality could be moved to another patch since it is unrelated. Or it could be replaced by a more descriptive exception.
I'm not even sure the initial bug is a very useful feature in and of itself. However, it is mentioned in the documentation for the method in question and therefore is quite confusing when it doesn't work.
-

GMFlash August 21st, 2008 @ 09:40 AM
- → State changed from open to resolved
Thanks for all the patches everyone!
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
