I have an array that I've made to have values added and removed on client side which then get persisted in the database when update action is triggered.
Adding/removing array values works excepts that with this approach, I cannot get rid of the last value of an array. Simply removing dom elements with Javascript and submitting this does nothing to the last value of an array.
I would like to be able to empty an array so with the page reload, blank field is presented for the first value entry.
Removing array value
$(document).on "click", ".delete_step", (event) ->
cache = $(this).closest(".step_set")
cache.fadeOut "fast", ->
cache.remove()
null
event.preventDefault()
Creating new field for new array value
$(document).on "click", "#add_step", (event) ->
html = "<div class='step_set'>" + "<input type='button' class='delete_step' value='Delete'>" + "<input type='text' name='recipe[steps][]'>" + "</div>"
$( "#steps" ).append html
event.preventDefault ()
Controller
def update
if @recipe.update_attributes!(recipe_params)
respond_with(@recipe)
else
render 'edit'
end
end
def recipe_params
params.require(:recipe).permit(:title, :image, :serving, { steps: [] }, ingredients_attributes: [:id, :name, :_destroy])
end
View
<%= simple_form_for @recipe do |f| %>
<button class="add" id="add_step">
<span>+</span>Add Step
</button>
<fieldset id="steps">
<% @recipe.steps.each_with_index do |individual_step, index| %>
<div class="step_set" id="step_set<%= index %>">
<a class="delete_step">Delete</a>
<input type="text" value="<%= individual_step %>" name="recipe[steps][]">
</div>
<% end %>
</fieldset>
<% end %>
After removing this element with delete and update

I still see this in the show page.

parents('.step_set:first')and could you give some example in jsfiddle?recipe_params[:recipe][:steps]. steps are JSON/JSONB/HStore collection?