2

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!

Community
  • 1
  • 1
user1224344
  • 129
  • 2
  • 11

1 Answers1

0

You are not checking if @registration.save actually saves the record. It can return true or false. I guess it just silently fails.

If you use @registration.save!, it wile raise an exception when anything goes wrong. I guess there is some kind of validation error there.

iblue
  • 29,609
  • 19
  • 89
  • 128
  • Well that definitely helped me and you are right, there is a validation error: Title can't be blank. The parameters state: "registrations"=>[{"title"=>{"registration"=>"Mrs"}, Which would seem that part of the form is badly formated perhaps, as it should just say: "title"=>"Mrs", What I'll for now is remove validations and see what happens then. – user1224344 Jul 02 '12 at 19:43