I have to submit multiple forms, I followed the advice of this post: How to submit multiple, duplicate forms from same page in Rails - preferably with one button
Note I'm still quite new to Rails/programming and some of my ways of doing things might not be ideal.
Here's my view:
= form_tag ([@event, @registration]) do
- x.times do
= render 'multi_form'
= submit_tag "Submit registrations"
The form (note that there are more fields):
- hidden_field_tag :event_id, :value => @event.id
.control-group
= label_tag :title
.controls
= select("registrations[][title]", :registration, Registration::TITLE)
.control-group
= label_tag :first_name
.controls
= text_field_tag "registrations[][first_name]"
.control-group
= label_tag :last_name
.controls
= text_field_tag "registrations[][last_name]"
.control-group
= label_tag :email
.controls
= text_field_tag "registrations[][email]"
The controller:
def create
array_number = 0
x.times do
@registration = Registration.new(params[:registrations][array_number])
@registration.save
UserMailer.registration_user_notify(@event, @registration).deliver
array_number = array_number + 1
end
respond_to do |format|
format.html {redirect_to thank_you_event_registrations_path(@event)}
end
end
When submitting it seems, to an extent, to be doing the right thing, for one it fires off an email to x unique email addresses, which makes me think that @registration contains the correct details in each loop - it's not saving to the database however. I can see that all the params are there in the log file, except that :title seems to be doing something bad (see below: but I'll focus on that later), the main thing I want it to do now is run though each array and save it as a new entry.
The log:
Parameters: {"utf8"=>"â", "authenticity_token"=>"BQXm5fngW27z/3Wxy9qEzu6D8/g9YQIfBL+mFKVplgE=", "event_id"=>"7", "registrations"=>[{"title"=>{"registration"=>"Mrs"}, "first_name"=>"Person", "last_name"=>"One", "email"=>"charl@privatelabel.co.za"...
I'm hoping the info I provided is enough, any advice will be appreciated.
Thanks!
EDIT:
@iblue
It did the trick! It was a validation error and it's saving everything into different rows. Thank you very much!
One more thing if I may, any idea how the :title form part should be formatted in order to return paramater:
"title"=>"Mrs",
as opposed to:
"registrations"=>[{"title"=>{"registration"=>"Mrs"},
Thanks again!