I have the following snippet in my app: <% @anitem = Item.find(:all, :conditions => "id = " + params[:id]) %> <% form_for :anitem, :url => { :action => :updateitem } do |thisform| -%> ... ... (some code...removed) ... <%= collection_select :thisform, :item_category, ItemCategory.find(:all), :id, :name %><br> <%= collection_select :thisform, :packing_list_type, PackingListType.find(:all), :id, :name %> <%= submit_tag ''Update'' %> <% end %> My issue is that the list box never selects the current value when it pulls the data from the database. Everything looks great - but no currently selected option tag ("selected"). Should be easy, but I can''t get it to work. Any ideas? -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-15 16:15 UTC
Re: List Box Issue - Should be easy, but nor for me!
On Dec 15, 1:37 pm, Ted Ster <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have the following snippet in my app: > > <% @anitem = Item.find(:all, :conditions => "id = " + params[:id]) %> > <% form_for :anitem, :url => { :action => :updateitem } do |thisform| > -%> > ... > ... (some code...removed) > ... > <%= collection_select :thisform, :item_category, > ItemCategory.find(:all), :id, :name %><br> > <%= collection_select :thisform, :packing_list_type, > PackingListType.find(:all), :id, :name %> > <%= submit_tag ''Update'' %> > <% end %> >When using the form_for stuff you need to do thisform.collection_select :field_name,... Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Well the documentation shows an example of how to select an element: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M000928 Do a view source and see if there is "selected" for an option. Put a debug statement and look at the log file to check if there is any value that is returned by the db. On Dec 15, 2007 5:37 AM, Ted Ster <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have the following snippet in my app: > > <% @anitem = Item.find(:all, :conditions => "id = " + params[:id]) %> > <% form_for :anitem, :url => { :action => :updateitem } do |thisform| > -%> > ... > ... (some code...removed) > ... > <%= collection_select :thisform, :item_category, > ItemCategory.find(:all), :id, :name %><br> > <%= collection_select :thisform, :packing_list_type, > PackingListType.find(:all), :id, :name %> > <%= submit_tag ''Update'' %> > <% end %> > > My issue is that the list box never selects the current value when it > pulls the data from the database. > > Everything looks great - but no currently selected option tag > ("selected"). > > Should be easy, but I can''t get it to work. > > Any ideas? > -- > 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 -~----------~----~----~----~------~----~------~--~---
> > When using the form_for stuff you need to do > thisform.collection_select :field_name,... >Thanks, Fred. Couldn''t get that to work. I tried various permutations. I am using this, and it works, but it''s a hack of sorts: <%= select(:aform, "packing_list_type_id", PackingListType.find(:all).collect {|p| [ p.name, p.id ] }, {:selected => @anitem[0][''packing_list_type_id'']}) %> It uses the :selected thingy to make it all work. - tedstur -- 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 -~----------~----~----~----~------~----~------~--~---