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:
....
...
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:
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:
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.
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.
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
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:
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.