rctneil
rctneil

Reputation: 7210

Multiple form_tags on one page

Say I have two form_tag 's on a page, each like this:

  = form_tag({:action => :import}, :multipart => true) do
    = file_field_tag 'file'
    = submit_tag 'Import', name: 'import_this'

So I know how that comes through in params but If I had two forms and each form had same name fields, how can I sort of namespace them in the resulting params array and how would I access them?

Upvotes: 0

Views: 1475

Answers (1)

Cristian
Cristian

Reputation: 6087

In order to differentiate between the two forms, you would need to create different names for the fields.

The most common practice is to nest the fields into a 'namespace', like this:

  = form_tag({:action => :import}, :multipart => true) do
    = file_field_tag 'form1[file]'
    = submit_tag 'Import', name: 'form1[import_this]'

And the second form:

  = form_tag({:action => :import}, :multipart => true) do
    = file_field_tag 'form2[file]'
    = submit_tag 'Import', name: 'form2[import_this]'

Then in the controller, you would access them like this:

params[:form1][:file]

or

params[:form2][:file]

Remember that you can't submit both forms at the same time.

Upvotes: 3

Related Questions