0

我正在制作一个简单的应用程序,应该使用rails 4动态删除字段,我基本上是按照rails cast教程做的,但是它在rails 3中,我设法解决了大部分问题,感谢上帝,但我被困在了这个,删除链接正确显示它只是不起作用!

当我去localhost:3000/survey/new每件事看起来都很好并且删除链接应该是

当我单击删除 url 时,只是从更改localhost:3000/survey/newlocalhost:3000/survey/new#

这是我的代码的一部分,我认为它与删除链接有关

#_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();
}

任何帮助,因为我不知道该做什么或缺少什么

4

1 回答 1

3

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

于 2014-04-14T08:49:15.117 回答