2

I'm trying to submit an array of emails via a form_tag in Rails 4.

new.html.erb:

...
<%= render partial: 'add_user' %>
<%= render partial: 'new_ride_form' %>
...

_add_user.html.erb:

...
            <input type='text' id='add_passenger_textfield'/>
            <button type='button' id='add_passenger_button'>Add</button>
...

_new_ride_form.html.erb:

<%= form_tag("/rides", method: "post", id: 'create_ride_submission_form') do %>
<div class="row" id="first_passenger">
...
</div>

<div class="row">
    <button id= 'create_ride_button'>Create Ride</button>
</div>

<% end %>

rides.js:

$(document).on('click', '#add_passenger_button', 
        function() {
          var v = $('#add_passenger_textfield').val();
          if(v.length > 0) {
            $('#first_passenger').after( 
              $('<div class="row"/>').append(
                $('<div>').append(
                  $('<input name="emails[]" type="text" value=' + v + ' disabled/>'))
                ));
          }
        }).on('click', '#create_ride_button', function() {
                ...
              $('#create_ride_submission_form').submit();
            }
        });

I want to get params[:emails] visible to the controller.

Before, when I just statically included something like

<text_field_tag 'emails[]'>

it seemed to work fine, but now the POST doesn't even send a field called "emails" anymore. Is this because I'm not using a form helper for the input?

1 Answer 1

3

Disabled form elements are not getting send to the server. Your input element in the javascript part is disabled and unless you're enabling it somewhere else it won't reach your controller ever.

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

1 Comment

Wow, as usual the mistake is very stupid :) I changed it from 'disabled' to 'readonly' and it works now! Thanks!

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.