I'm new to rails (coming from PHP) and am having an issues with submitting a form with ajax. Basically, I have a list of tasks, and when a new one is added I want the new task to append to the list. The form itself is submitting, but I can't figure out how to append the new entry. Thanks in advance!
THE FORM
<%= simple_form_for(Task.new) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :date_due %>
<%= f.input :phase_id, :as => :hidden, :input_html => { :value => @phase.id } %>
<%= f.input :status, :as => :hidden, :input_html => { :value => "0" } %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
THE JS
$('form#new_task').submit(function() {
var valuesToSubmit = $(this).serialize();
$.ajax({
type: "POST",
url: $(this).attr('action'), //sumbits it to the given url of the form
data: valuesToSubmit,
dataType: "JSON"
}).success(function(data){
// disaply new charge in the table
$('body').append(data);
// clear the form
$("input#task_name").val('');
//focus on the input again
$("input#task_name").focus();
});
return false; // prevents normal behaviour
});
EDIT It's also worth mentioning that this is happening inside a view for a "Phase". Each phase has_many tasks and each task belongs_to a phase.
puts paramsWhat do you mean by how to append to a new entry?tableor whatever you're rendering the list of objects in.