Help with custom input: select

123 views
Skip to first unread message

Brian Jensen

unread,
Sep 16, 2012, 4:53:19 PM9/16/12
to plataformate...@googlegroups.com
Hi Guys.

I have this: 

= f.input :user_id, :collection =>  client.users, :required => true

But I need to be able to produce their data-created-at with each option as well.

So it would be

<select>
<option data-created-at="2012-09-13" value="1">John Doe</option>
<option data-created-at="2012-09-10" value="2">Michael Madsen</option>
</select>

My first though is to create a custom input...

When I check out the source it seams that the rendering of the select handled in action_view_extensions/builder.rb:286, but I don't know I can override this behavior to include data-created-at on each option.

Help please.

Vasiliy Ermolovich

unread,
Sep 16, 2012, 5:02:43 PM9/16/12
to plataformate...@googlegroups.com
Hi Brian. I think right you can't do it with SimpleForm's input but you can do it with Rails's select. Check out this sample:

<%= simple_form_for(@user) do |f| %>
<%= f.input :group_id, :collection => [1,2,3] do %>
<%= f.select :group_id, [1,2,3].map { |i| [i, i, {:"data-created_at" => Time.now }] }%>
<% end %>
<%= f.button :submit %>
<% end %>

So as you can see you can pass a hash as a third element of array.

Brian Jensen

unread,
Sep 16, 2012, 5:20:29 PM9/16/12
to plataformate...@googlegroups.com
Cool! Thanks for the insanely quick reply(within 2 minuttes) and the perfect answer to my question :-)

Best regards
Brian

Vasiliy Ermolovich

unread,
Sep 16, 2012, 5:21:55 PM9/16/12
to plataformate...@googlegroups.com
Glad to help! :)
Reply all
Reply to author
Forward
0 new messages