0
我是Ruby on Rails的初學者,也是Web開發的初學者。我正試圖學習Railscasts ep 196 - 198上的Neste表單。直到javascript和helper部分,我才明白這個情節。我仍然可以理解JavaScript中的「add_fields」函數,但是當他開始鏈接這些方法時,我只是不知所措。任何人都可以幫助和指導這個白癡嗎?Ruby on Rails嵌套模型細分
application_jquery.js:
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
助手/ application_helper.rb:
module ApplicationHelper
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
end
end
順便說一下,我使用Rails 4,如果有幫助。