2

In my view I need to add (dynamically) text inputs and I need to get theirs values in the controller (once the user submit the corresponding form).

My inputs are:

<input type="text" name="airports_input_origin" class="airports_input_origin" id="airports_input_origin_0" size="40"/> 

<input type="text" name="airports_input_origin" class="airports_input_origin" id="airports_input_origin_1" size="40"/>

etc... etc...

They all have the same 'name' attribute.... so I guessed that if I do

params[:airports_input_origin]

I'd get the array ... but I was wrong...

How can I get those values?

Thx!

1 Answer 1

6

You'd have to do something like so:

<input type="text" name="airports_input_origin[0]" class="airports_input_origin" id="airports_input_origin_0" size="40"/> 

<input type="text" name="airports_input_origin[1]" class="airports_input_origin" id="airports_input_origin_1" size="40"/>

Rails understand that it's an array if you tag numbers like that within the name.

So you could have a JavaScript function that iterates over all the inputs and resets their numbers everytime a destination is added/deleted. Use regex to replace the numbers or you could probably even hard code the name and change the "[x]" if this is all that's required.

Sign up to request clarification or add additional context in comments.

1 Comment

and the I should be able to params[:airports_input_origin].size or params[:airports_input_origin][0] ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.