Jeff Lee
2008-Jun-10 02:55 UTC
Select list for Pragmatic multiple models one form tutorial
After going through the multiple models one form tutorial at Pragmatic (http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf) and the one from Rails casts, I''ve chosen to use the one at Pragmatic for its simplicity. Everything is easy to understand and works pretty well; however, I''m having some trouble with select lists. The pertinent code is below: _email.html.erb: <div class="email"> <% new_or_existing = email.new_record? ? ''new'' : ''existing'' %> <% prefix = "profile[#{new_or_existing}_email_attributes][]" %> <% fields_for prefix, email do |email_form| -%> <p> Email: <%= email_form.text_field :address %> Device: <%= collection_select (:email, :device_id, Device.find(:all), :id, :name, options ={:prompt => "Select a device"}, html_options ={:class => "device", :name => "profile[#{new_or_existing}_email_attributes][][device_id]"}) %> <%= link_to_function "remove" , "$(this).up(''.email'').remove()" %> </p> <% end -%> </div> The fields in this form are given names and ids such that they either have the index and new or existing: <div class="email"> <p> Email: <input id="profile_existing_email_attributes_11_address" name="profile[existing_email_attributes][11][address]" size="30" type="text" value="myemail-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" /> Device: <select class="device" id="email_device_id" name="profile[existing_email_attributes][][device_id]"> <option value="">Select a device</option> <option value="2">Fax</option> <option value="3">Mobile</option> <option value="5">Office</option> <option value="6">Home</option> </select> <a href="#" onclick="$(this).up(''.email'').remove(); return false;">remove</a> </p> </div> As you may notice, I just stuck the name in for the device_id select field. When I do this, it does not preselect the existing value (this is a huge pain). The tutorial suggests the following for a simple select: <%= task_form.select :completed, [[''No'' , false], [''Yes'' , true]] %> which would be something like this in my variation: <%= email_form.select :device_id, [[''No'' , false], [''Yes'' , true]] %> But, I do not know how I can provide my select options to this using a find. When I include this suggested code the name and id are appropriate and the selected item is correct. This is obviously not the ideal solution as it defeats the purpose of having my options in a table (they do need to be dynamic). As a side note, at this point in time (deadlines) the application needs to be finished. Thus, I am unable to keep "improving" the code with newer and better methods or ways of doing things. While I would enjoy other options to develop multiple models in one form and may implement them at a later date, I would sincerely appreciate ways to solve this select issue while maintaining the rest of the code (which is close to the tutorial). Any help is appreciated! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeff Lee
2008-Jun-10 05:48 UTC
Re: Select list for Pragmatic multiple models one form tutorial
I''ve figured out how to accomplish what I''ve asked so I thought it might be useful for others. To generate the select list as the tutorial would want you would use the select method: <%= email_form.select :device_id, Device.find(:all).collect {|p| [ p.name, p.id ] }, { :prompt => "Select a device" } %> On Jun 9, 9:55 pm, Jeff Lee <jle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> After going through the multiple models one form tutorial at Pragmatic > (http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf) > and the one from Rails casts, I''ve chosen to use the one at Pragmatic > for its simplicity. Everything is easy to understand and works pretty > well; however, I''m having some trouble with select lists. > > The pertinent code is below: > > _email.html.erb: > <div class="email"> > <% new_or_existing = email.new_record? ? ''new'' : ''existing'' %> > <% prefix = "profile[#{new_or_existing}_email_attributes][]" %> > <% fields_for prefix, email do |email_form| -%> > <p> > Email: <%= email_form.text_field :address %> > Device: <%= collection_select (:email, :device_id, > Device.find(:all), :id, :name, options ={:prompt => "Select a > device"}, html_options ={:class => "device", :name => > "profile[#{new_or_existing}_email_attributes][][device_id]"}) %> > <%= link_to_function "remove" , "$(this).up(''.email'').remove()" %> > </p> > <% end -%> > </div> > > The fields in this form are given names and ids such that they either > have the index and new or existing: > > <div class="email"> > <p> > Email: <input id="profile_existing_email_attributes_11_address" > name="profile[existing_email_attributes][11][address]" size="30" > type="text" value="myem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" /> > Device: <select class="device" id="email_device_id" > name="profile[existing_email_attributes][][device_id]"> > <option value="">Select a device</option> > <option value="2">Fax</option> > <option value="3">Mobile</option> > <option value="5">Office</option> > <option value="6">Home</option> > </select> > <a href="#" onclick="$(this).up(''.email'').remove(); return > false;">remove</a> > </p> > </div> > > As you may notice, I just stuck the name in for the device_id select > field. When I do this, it does not preselect the existing value (this > is a huge pain). The tutorial suggests the following for a simple > select: > > <%= task_form.select :completed, [[''No'' , false], [''Yes'' , true]] %> > which would be something like this in my variation: > <%= email_form.select :device_id, [[''No'' , false], [''Yes'' , true]] %> > > But, I do not know how I can provide my select options to this using a > find. When I include this suggested code the name and id are > appropriate and the selected item is correct. This is obviously not > the ideal solution as it defeats the purpose of having my options in a > table (they do need to be dynamic). > > As a side note, at this point in time (deadlines) the application > needs to be finished. Thus, I am unable to keep "improving" the code > with newer and better methods or ways of doing things. While I would > enjoy other options to develop multiple models in one form and may > implement them at a later date, I would sincerely appreciate ways to > solve this select issue while maintaining the rest of the code (which > is close to the tutorial). > > Any help is appreciated!--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---