I have a Rails 4 form that builds a list of parts using AJAX on the form page. Once the list of parts is built in the <ul> I want to submit the list as an array of values in a parameter in the params hash.
My form:
<%= form_for ([@tool, @service]),:html => { :onSubmit => 'getParts' } do |f| %>
<% if @service.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@service.errors.count, "error") %> prohibited this service from being saved:</h2>
<ul>
<% @service.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="span4 offset1">
<div class="form-inline">
<%= f.hidden_field :tool_id , :value=>params[:tool_id] %>
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<br>
<br>
<div class="form-inline">
<%= f.label :due_date %><br>
<%= f.text_field :due_date, 'data-behaviour' => 'datepicker' %>
</div>
<br>
<br>
<div class="form-inline">
<%= f.label :completed %><br>
<%= f.text_field :completed, 'data-behaviour' => 'datepicker' %>
</div>
<br>
<div class="field">
<%= f.label 'Service Type:' %>
<%= f.select :service_type_id, ServiceType.all.collect{|s| [s.name, s.id] }, {include_blank: false} %>
</div>
<br>
</div>
<div class="span7">
<div class="form-inline">
<div class="part_list_element">
Parts Used <br>
<%= text_field_tag :parts_used %>
</div>
</div>
<br>
Default Parts:
<ul id="serv_parts_list">
</ul>
<br>
<br>
<div class="form-inline">
<%= f.label :note %><br>
<%= f.text_area :note %>
</div>
<br>
<br>
<div class="actions">
<%= f.submit %>
</div>
</div>
</div>
<% end %>
When a user chooses a Default Service from the selection list, this javascript fires:
$('#service_service_type_id').change(function() {
var id = this.value;
$.ajax
({
url:'/get_default_parts',
type:"POST",
data: {
service_type: {
id: id
}
}
});
});
This gets a list of parts from the DB based on the selection. It then returns an array via JS that then gets appended to the empty Default Parts list:
Default Parts:
<ul id="serv_parts_list">
</ul>
becomes:
<ul id="serv_parts_list">
<li id="042511060272">042511060272 - Stihl/Denso Spark Plug W22mp-u<a class="service_parts"><-Remove</a></li>
<li id="795711145835">795711145835 - Bar Oil - Stihl<a class="service_parts"><-Remove</a></li>
<li id="795711478179">795711478179 - Stihl MS660/066 Air Filter<a class="service_parts"><-Remove</a></li>
</ul>
In my application.js I have:
$(document).ready(function(){
$("#commit").click(function() {
$("#expended_parts").val(('#serv_parts_list').text());
});
});
But this doesn't do anything. I've been trying to figure out how to get the <li> elements in the <ul> into an array and passed as a param. But current params hash looks like:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"s/....=", "service"=>
{"tool_id"=>"113", "name"=>"test without hash", "due_date"=>"", "completed"=>"",
"service_type_id"=>"2", "note"=>""}, "parts_used"=>"", "commit"=>"Create Service"}
Since the list of parts can get updated multiple times after the page loads, either from an AJAX call to get the default parts list that gets appended to the <li>, I think this would have to be bound to the submit event. Any help is appreciated.