Hi guys, I just recently fell in love with Rails. Now, I''m trying to make a simple page where at list of tasks is presented. Each task has a user. At the top of the list I want to be able to make a select/dropdown list containing all users, so that will be able to select all tasks for a certain user. Looking at the Rails api I find the helper method: collection_select. Just what I need! There is just one problem. I want to select-tag to be named "user_id". However, according to the documentation you must specify a "object" and a "method". For example: <%= collection_select ''test'', ''boing'', @users, ''id'', ''name'' %> Outputs: <select id="test_boing" name="test[boing]"> <option value="1">Rasmus</option> <option value="2">Dennis</option> </select> This just doesn''t make any sense to. How come I can''t set the name of the select tag to for example ''user_id'' so that the path when search will be ''/tasks?user_id=1'' instead of ''/tasks?test%5Bboing%5D=2''. Am I taking a "non-rails approach" to this problem? How can I set the name of the select-tag to "user_id"? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I achieved the wanted result like this:
<%= select_tag ''user_id'', options_for_select(@users.collect
{ |u|
[u.name, u.id] }) %>
However, I guess you''ll agree it''s not the most beautiful code
ever
written. Any suggestions for improvement?
Thanks in advance.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Rasmus Nielsen wrote:> I achieved the wanted result like this: > <%= select_tag ''user_id'', options_for_select(@users.collect { |u| > [u.name, u.id] }) %> > > However, I guess you''ll agree it''s not the most beautiful code ever > written. Any suggestions for improvement? > > Thanks in advance.You might try options_from_collection_for_select: http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---