Form collects multiple parameters from a select list (multiple enabled) what kind of code do i need in the controller ? Stuart -- http://en.wikipedia.org/wiki/Dark_ambient --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/22/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Form collects multiple parameters from a select list (multiple enabled) > what kind of code do i need in the controller ? > > StuartNot making much progress here , so let me give more detail: Form has this element - select name=canindustry[category_id][] size="6" multiple="multiple"> <%= options_from_collection_for_select @categories, :id, :name %> Params being submitted (example) *Parameters*: {"commit"=>"Create", "canindustry"=>{"category_id"=>["2", "3", "4", "5"]}, The fields in the canindustry model are: id | candidate_id | category_id I want to insert multiple rows , so using above parameters id | candidate_id | category_id | 1 11 2 2 11 3 3 11 4 4 11 5 Problem is in my controller where i"ve tried a few different ways with no success: Right now I have this in the create action: @canindustry.candidate_id = current_user.id a = (params[:canindustry]) a.each {|x| a.canindustry.save } and getting back an error - "You have a nil object when you didn''t expect it! The error occurred while evaluating nil.candidate_id=" ??? Stuart --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Not much around on this googling. Anyone ever do this before ? On 10/22/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On 10/22/06, Dark Ambient <sambient-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Form collects multiple parameters from a select list (multiple enabled) > > what kind of code do i need in the controller ? > > > > Stuart > > > Not making much progress here , so let me give more detail: > > Form has this element - > select name=canindustry[category_id][] size="6" multiple="multiple"> > <%= options_from_collection_for_select @categories, :id, :name %> > > Params being submitted (example) > *Parameters*: {"commit"=>"Create", "canindustry"=>{"category_id"=>["2", > "3", "4", "5"]}, > > The fields in the canindustry model are: id | candidate_id | > category_id > I want to insert multiple rows , so using above parameters > > id | candidate_id | category_id | > 1 11 2 > 2 11 3 > 3 11 4 > 4 11 5 > > Problem is in my controller where i"ve tried a few different ways with no > success: > Right now I have this in the create action: > > @canindustry.candidate_id = current_user.id > > a = (params[:canindustry]) > a.each {|x| a.canindustry.save > } > > and getting back an error - > > "You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.candidate_id=" > > ??? > Stuart >-- http://en.wikipedia.org/wiki/Dark_ambient --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Dark Ambient wrote:> > > > > Not making much progress here , so let me give more detail: > > > > Form has this element - > > select name=canindustry[category_id][] size="6" multiple="multiple"> > > <%= options_from_collection_for_select @categories, :id, :name %> > > > > Params being submitted (example) > > *Parameters*: {"commit"=>"Create", "canindustry"=>{"category_id"=>["2", > > "3", "4", "5"]}, > > > > The fields in the canindustry model are: id | candidate_id | > > category_id > > I want to insert multiple rows , so using above parameters > > > > id | candidate_id | category_id | > > 1 11 2 > > 2 11 3 > > 3 11 4 > > 4 11 5 > > > > Problem is in my controller where i"ve tried a few different ways with no > > success: > > Right now I have this in the create action: > > > > @canindustry.candidate_id = current_user.id > >I assume you set @canindustry somewhere to a proper object, but the error stated otherwise.> > a = (params[:canindustry]) > > a.each {|x| a.canindustry.save > > } > >Here I think params[:canindustry][:category_id] will give you the array [] of selected category ids. Each ''a'' element is a String, which dnu #canindustry let alone #save. Seems to me you need something like the following in your a.each block. canindustry = Foo.new canindustry.candidate_id = current_user.id canindustry.category_id = x canindustry.save Long www.edgesoft.ca --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 10/22/06, Long <long755-bJEeYj9oJeDQT0dZR+AlfA@public.gmane.org> wrote:> > > Dark Ambient wrote: > > > > > > > > Not making much progress here , so let me give more detail: > > > > > > Form has this element - > > > select name=canindustry[category_id][] size="6" multiple="multiple"> > > > <%= options_from_collection_for_select @categories, :id, :name %> > > > > > > Params being submitted (example) > > > *Parameters*: {"commit"=>"Create", > "canindustry"=>{"category_id"=>["2", > > > "3", "4", "5"]}, > > > > > > The fields in the canindustry model are: id | candidate_id | > > > category_id > > > I want to insert multiple rows , so using above parameters > > > > > > id | candidate_id | category_id | > > > 1 11 2 > > > 2 11 3 > > > 3 11 4 > > > 4 11 5 > > > > > > Problem is in my controller where i"ve tried a few different ways with > no > > > success: > > > Right now I have this in the create action: > > > > > > @canindustry.candidate_id = current_user.id > > > > I assume you set @canindustry somewhere to a proper object, but > the error stated otherwise.Yeah , it was set up as a proper object though I was getting all sorts of errors.> > a = (params[:canindustry]) > > > a.each {|x| a.canindustry.save > > > } > > > > Here I think params[:canindustry][:category_id] will give you the array [] > of selected category ids. > Each ''a'' element is a String, which dnu #canindustry let alone #save. > > Seems to me you need something like the following in your a.each block. > > canindustry = Foo.new > canindustry.candidate_id = current_user.id > canindustry.category_id = x > canindustry.saveYep , it works ! Thank you! a = (params[:canindustry][:category_id]) a.each {|x| canindustry = Canindustry.new canindustry.candidate_id = current_user.id canindustry.category_id = x canindustry.save } Let this thread serve as a warning jumping to fast into Rails without having Ruby array methods burned deeply in your brain. Stuart> >-- http://en.wikipedia.org/wiki/Dark_ambient --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---