input_html options for each item in a collection
It would be useful to be able to add a css class or data attribute to each item in a collection, based on a lambda.
(as you can currently do with :label_method + :value_method)
Perhaps, do you have any specific use case?
A country select list, certain countries in the list are VAT eligible, the lambda compares items against another array and adds a data-attribute if there's a match.
This makes, for example, showing / hiding other parts of the form based on the selection very easy with JavaScript.
I see your point. The problem with the select list is that it just delegates to Rails collecion_select helper, and we do not have control over the select options being created, given they delegate to options_from_collection_for_select and so on. Basically, this is not in SimpleForm hands, unless we recreate this helper entirely.
I'm unable to work on this right now, but I'd be happy to receive a pull request if you want to do some work on it =). Thanks.
This is basically a fundamental feature based on experience in last 3 projects. I considered making a patch myself recently but as it's for some reason outside of simple form scope I'll monkey-patch it, I bet most people will do this - which is bad. =S
Test + possible solution for this here:
https://github.com/pivotal-casebook/simple_form/commit/f134b1c606952040d90d0bbbbe7deff7f0dc5f61
Not sure if a proc is the greatest way to solve this but like carlos said part of the problem is collection_select gives you no control over these things but a select tag will provided you do all the work with your own options.
May submit a pull request but I think documentation should be added along with some clarity around what to do if you provide other options that would be ignored (such as :label_method etc etc)
@pivotal-casebook: I'm not sure this is very good solution (sorry, not trying to be rude), a per-item-proc would make so much more sense - but yes it requires patching Rails form helpers. My personal opinion is that Rails core form helpers are kind of useless anyway - why are we all using simple_form or formtastic to start with? Don't know anyone relying on the core ones - so overriding the core ones probably is a must either way.
@pivotal-casebook although your solution is simple (I really haven't thought about it), I have to agree with @grimen that a per-item-proc seems more reasonable. I would not like to recreate the entire collection_select feature, but I can't see other way =(
grimen I've just arrived at this dilemma too http://groups.google.com/group/plataformatec-simpleform/browse_thread/thread/286adefdf4656b05
If you have any done any work on it so far or monkey patching etc I'd love to see.
Thanks
Forgive my ignorance but could it have something to do with this? See the bottom comment
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select#1030-helpers-using-options-for-select
options_for_select([["Dollar", "$", {:class=>"bold"}], ["Kroner", "DKK", {:onclick => "alert('HI');"}]])
Edit just realised that it's only collection_select that matters here
Edit 2 incase anyone else reads this thread. This is working perfectly for my needs using pivotal's changes (thanks!)
<%= f.input :project, :collection => Proc.new{ Project.all.map{ |p| [p.name, p.id, :class => "project-#{p.id}"] } } %>
Yes, you're completely right! This was added to Rails 3, you're able to give a hash of options to the collection in the array form. So giving a collection like this (from SF test suite):
:collection => [['Jose', 'jose', { :class => "bar" }], ['Carlos', 'carlos', { :id => "baz" }]]
Would output a select like this:
<select class="select required" id="user_name" name="user[name]">
<option value=""></option>
<option value="Jose" class="bar">Jose</option>
<option value="Carlos" id="baz">Carlos</option>
</select>
It means you can give any options to the items in the collection, and we won't need to do any work for that =). Thanks mate.
Btw, just saw your edit2 example, you shouldn't need the patch + use Proc.new, just giving the collection with Project.all.map as you did should work fine.
Awesome! Ah yes of course, it works just with map like you said. Thanks Carlos
Hmm...interesting, I've totally missed that was added to Rails 3. Great, this means I can nuke some ugly code then!
Not sure if I completely misunderstood you guys conclusions, but I run Rails 3.0.5 and I now got back to this "problem" in a project and I tried this example:
payment.input :method, :as => :radio, :collection => [['Bar', 'bar', {:class => "bar"}], ['Baz', 'baz', {:id => "baz"}]]
...renders:
http://pastie.org/1745868 (forgot how to escape HTML in GH-comments :P)
How did you manage to get a valid output? Are you guys running 3.1.0.beta?
@grimen sorry mate, I guess I should've explained it better. This implementation only works for selects as it's done inside Rails form options helpers. SimpleForm collection helpers (radio / check boxes) do not handle this third option, although we could think about implementing it somehow. If you think it'd be useful, I guess we should open another issue explaining how it'd be and close this one. What do you think?
@carlosantoniodasilva Ah, I see - yes I think my assumption says that this shoould work if it is expected to work for selects. Would love to discuss a solution on this, though I need to refresh my memory on the simple_form codebase - haven't peeped it in long time now. :)
Just noted in my app (and it is visible on the example above):
:collection => [['Jose', 'jose', { :class => "bar" }], ['Carlos', 'carlos', { :id => "baz" }]]
Would output a select like this:
<select class="select required" id="user_name" name="user[name]">
<option value=""></option>
<option value="Jose" class="bar">Jose</option>
<option value="Carlos" id="baz">Carlos</option>
</select>
The VALUE for each option is incorrectly taken from first array member, not the second (you can see it is capitalized...). Vanilla Rails 3 options_for_select works OK.
With relation to the previous post. I'm finding this behaviour too. I tried to find a reason in the code but got a bit lost. Would someone be able to provide some insight into why collection options structured as above don't allow for the value to be defined in the second array element?
@brendon Actually, I've never used it this way, maybe it's a SimpleForm issue. Have you tried using only :collection_select with a collection having such options, to see if it works fine? If it works, we can look at SimpleForm to find out where the issue is. Thanks.
Thanks Carlos,
From my experiments, I think it has something to do with the value_method and input_method defaults. Normally :first and :last I think they may be defaulting to :first and :first because the last item in this case is a Hash. I tried setting it to :first and :second and that worked, but it then ignored my Hash (i.e. didn't pass it through as an options hash). That's about as far as I got.
I was trying to find if there was a way to manually pass the select html straight from the rails helper into SimpleForm, and from what you say it sounds like :collection_select is the way to do this? I'll give it a go when I get to work and see how that goes. If it works, I can't see any reason why SimpleForm needs to change :D
Have a great day!
Ah, I see. I tried calling collection_select and it had the same incorrect results as through simple_form when I define the value_method and text_method.
Using options_for_select works properly though. Does simple_form use this in any way?
Is there a way to supply pre-generated options from options_for_select to the simple_form input helper? :)