I have table which stores multiple entries. Each row has user_id, game_id and a pick_id. On a page the user has to make load of choices. All this gets submitted. the html looks like so: <input type="hidden" name="game_id" value="3"> <input type="hidden" name="user_id" value="19"> <select name="pick[3]"> .... </select> <input type="hidden" name="game_id" value="4"> <input type="hidden" name="user_id" value="19"> <select name="pick[4]"> ... </select> on and on... how do i get the controller to take all the form elements and do a multiple insert into the db? -- Posted via http://www.ruby-forum.com/.
mizage wrote:> I have table which stores multiple entries. Each row has user_id, > game_id and a pick_id. On a page the user has to make load of choices. > All this gets submitted. > the html looks like so: > > <input type="hidden" name="game_id" value="3"> > <input type="hidden" name="user_id" value="19"> > <select name="pick[3]"> > ..... > </select> > > <input type="hidden" name="game_id" value="4"> > <input type="hidden" name="user_id" value="19"> > <select name="pick[4]"> > .... > </select> > > on and on... > > how do i get the controller to take all the form elements and do a > multiple insert into the db? >Let''s say your table which stores multiple entries is named ''entries'' and the model name is Entry. From what you posted I am thinking you have the following table structure and this whole post is based around this structure. If this is incorrect please post more data about your problem and your setup: entries (tablename) --------------- pick_id (column) user_id (column) game_id (column) Since user_id, game_id and pick_id all belong to table entries we take advantage of rails. Rename your html form elements: * ''game_id'' to ''entry_game_id'' * ''user_id'' to ''entry_user_id'' * What field does pick[3] and pick[4] correspond to? Can they select multiple picks? Also give your fields a name attribute: * ''entry[game_id]'' * ''entry[user_id]'' * And possibly pick...i don''t know how you''re using that yet, so i can''t say for sure So now your html looks like: <input type="hidden" name="entry[game_id]" id="entry_game_id" value="3"> <input type="hidden" name="entry[user_id]" id="entry_user_id" value="19"> In your controller you can now do something like: entry = Entry.new params[''entry''] if entry.save flash[:notice] = ''yay it saved!'' else ... end Rails will parse the name/id tags of the inputted form elements and it will populate your Entry object for you if it follows the naming convention.. "modelname_fieldname" for the id tag and "modelname[fieldname]" for the name tag. Hope this helps, Zach
the table is about that except for the id column (which auto increments) and name.> picks (tablename) > ---------------id (column pick_id (column) user_id (column) game_id (column) There is one pick per game_id. So now the html is: <input type="hidden" id="pick.game_id" name="pick[game_id]" value="1"> <input type="hidden" id="pick.user_id" name="pick[user_id]" value="5"> <select id="pick_pick" name="pick[pick]"> ... </select <input type="hidden" id="pick.game_id" name="pick[game_id]" value="2"> <input type="hidden" id="pick.user_id" name="pick[user_id]" value="5"> <select id="pick_pick" name="pick[pick]"> ... </select ...plus another 40 fields which the controller now does this: pick = Pick.new(params[''pick'']) pick.save The first group of fields is saved to the db correctly. The other 41 do not get saved. Do I need to loop in my controller? cheers, etienne -- Posted via http://www.ruby-forum.com/.
ok i figured it out but it doesn''t "feel" very clean!! so if anyone has a better way, please let me know. Newbie muppet code can always be improved upon. The controller does this: i = 0 master = params[''pick''] #returns arrays of pick_ids, game_ids, user_id game_ids = master[''game_id''] #returns array of game_ids pick_ids = master[''pick_id''] #returns array of pick ids user_ids = master[''user_id''] #returns user_id game_ids.each do pick = Pick.new pick.game_id = game_ids[i] pick.pick_id = pick_ids[i] pick.user_id = user_ids pick.save i += 1 end -- Posted via http://www.ruby-forum.com/.
This is basically what I had to in a Java-equivalent program, and I don''t see any other solution. I am still new at Ruby though. Cleanliness, you could create a helper for this, it takes all the params, you tell it which group to look for and it iterates through that group, saving it for that Model. Thanks for posting your solution even though response was minimal, it helps others with the same problems. - Nic. On 12/14/05, mizage <estettler-O5WfVfzUwx8@public.gmane.org> wrote:> ok i figured it out but it doesn''t "feel" very clean!! so if anyone has > a better way, please let me know. Newbie muppet code can always be > improved upon. > > The controller does this: > > i = 0 > master = params[''pick''] #returns arrays of pick_ids, game_ids, > user_id > game_ids = master[''game_id''] #returns array of game_ids > pick_ids = master[''pick_id''] #returns array of pick ids > user_ids = master[''user_id''] #returns user_id > > game_ids.each do > pick = Pick.new > pick.game_id = game_ids[i] > pick.pick_id = pick_ids[i] > pick.user_id = user_ids > pick.save > i += 1 > end > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
UPDATE: Checking the Agile WD book, they have a similiar example with checkboxes relating to order that have shipped (pg 121). They create numbered checkboxes based on the order_id, and then parse through it in a loop in the Controller: A sample HTML for the checkbox is: <input name="to_be_shipped[1]" type ="checkbox" value="yes"/> Where the [1] portion is referring to the order_id, meaning this is the dynamic part So, in the controller, the code they have is: to_ship = params[:to_be_shipped] if to_ship to_ship.each do |order_id, do_it| if do_it == "yes" # mark order as shipped (DB work).... end end end On 12/14/05, Nic Werner <nicwerner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is basically what I had to in a Java-equivalent program, and I > don''t see any other solution. I am still new at Ruby though. > > Cleanliness, you could create a helper for this, it takes all the > params, you tell it which group to look for and it iterates through > that group, saving it for that Model. > > Thanks for posting your solution even though response was minimal, it > helps others with the same problems. > > - Nic. > > On 12/14/05, mizage <estettler-O5WfVfzUwx8@public.gmane.org> wrote: > > ok i figured it out but it doesn''t "feel" very clean!! so if anyone has > > a better way, please let me know. Newbie muppet code can always be > > improved upon. > > > > The controller does this: > > > > i = 0 > > master = params[''pick''] #returns arrays of pick_ids, game_ids, > > user_id > > game_ids = master[''game_id''] #returns array of game_ids > > pick_ids = master[''pick_id''] #returns array of pick ids > > user_ids = master[''user_id''] #returns user_id > > > > game_ids.each do > > pick = Pick.new > > pick.game_id = game_ids[i] > > pick.pick_id = pick_ids[i] > > pick.user_id = user_ids > > pick.save > > i += 1 > > end > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >