0

I have a text field that accepts any number of rows of input, with four columns. Valid input might be:

1 2 3 A
3 4 2 B
1 3 2 C
etc..

Normally with ActiveBase, fields would be generated by whatever model that you have using the form_for method, however, I do not wish to do that here, because then the user would have to fill out a form for each row of input, then submit, when instead I just want the user to paste any number of rows, and press submit once.

After submitting, a service call will be made with a JSON representation of all of the models that were created. I figured that having this textarea populate N number of models would ensure validation, and I could easily throw out bad requests.

I am unsure how to parse and post this data in 'simple rails fashion', given that the form isn't generated using form_for and the fact that multiple of the same model can be created.

So the real question is, how do I structure this? What does my workflow look like?

Matt M
  • 149
  • 2
  • 4
  • 17
  • Turns out that I need Virtual Attributes to solve the first part of this problem. The second part of this problem is to solve how to do multiple rows, and the third, on how to submit this data to the service. – Matt M Oct 10 '16 at 22:52
  • Hi and welcome to Stack Overflow. Have you tried making this form and seeing what comes through to your server when you post it? Then, see what you can do to iterate through the data... and use each row to create your model? Here we prefer you to have at least had a crack at solving your problem, then you can show us what you've done (even if it isn't working) plus any errors you got. This both shows that you're willing to put in some effort instead of us writing it for you ;) and also helps us to better understand what you're trying to do. – Taryn East Oct 10 '16 at 22:54
  • Have you tried form_tag? It's less opinionated about models than form_for. – eeeeeean Oct 11 '16 at 03:41

1 Answers1

0

Say you have a form you've written "by hand":

<form id="my_form" action="/my_endpoint" method="POST"

  <!-- you need a line like this to PUT, DELETE, or PATCH -->
  <input type='hidden' name='_method' value="PUT">

  <!-- if you enabled csrf check -->
  <input type='hidden' name='authenticity_token' value='<%= form_authenticity_token %>'>

  <!-- a base input -->
  <!-- send the param as an array by using [] in the name attribute -->
  <input id="base_input" type='text' name='user_inputs[]'>

  <input type='submit' value='submit'>
</form>

And you also have a button which adds an input to the form:

<button id="add_input">add input</button

You can write some jQuery to add fields to the form (this example is coffeescript):

# document ready block

$(->
  $("#add_input").on "click", (e) ->
    $form = $ "#my_form"
    $baseInput = $ "#base_input"
    $newInput = $baseInput.clone()
    # clear entered text
    $newInput.val ""
    # remove base_input id
    $newInput.attr "id", ""
    # add the new input after the base input
    $baseInput.after $newInput
)

In the controller, you'd have a params user_inputs which is an array of strings; what to do with this data is up to you. The base input can also be a different type (textarea, checkbox, etc) but in jQuery you'll have to use different javascript methods to clear their prior content.

max pleaner
  • 26,189
  • 9
  • 66
  • 118