Question

i am making a simple app that should remove fields dynamically using rails 4 , i am doing that basically bu following rails cast tutorial , but its in rails 3 , i manged to solve most of the problems thanks God but i am stuck in this one ,the remove link appears correctly it just don't work !!!

when i go to localhost:3000/survey/new every thing looks ok and the remove links are as it should be

when i click remove the url just change from localhost:3000/survey/new to localhost:3000/survey/new#

here is a part of my code that i think its related to the remove link

#_form.html.erb
<%= f.fields_for :questions do |ff| %>
<div class="field">
<%= render 'question_fields', :f => ff %>
</div>
<% end %> 

#_question_field.html.erb
<div class="field">
<p>
<%= f.label :content , "question"%><br>
<%= f.text_area :content %>
<%= link_to_remove_fields "remove", f %>
</p>
</div>

module ApplicationHelper
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
end

#application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).parent(".fields").hide();
}

any help as i have no clue what to do or what is missing

Was it helpful?

Solution

That Railscast has several flaws - the most prominent being it only allows single form elements to be appended to the page (it preloads the JS link with a form element & keeps putting the same element onto the page -- problem being this element has the same id as the others)

A better tutorial can be found here (uses child_index)


I would highly recommend looking at the Cocoon gem

You'll be able to see how it works with the link above, but the bottom line is you'll be able to use this to call as many new form elements as you need: link_to_add_association, or link_to_remove_association

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top