use*_*363 0 jquery ruby-on-rails ruby-on-rails-3.1
这是raislcasts.com复杂形式的一个函数 - 关于动态地将字段添加到表单的示例.该函数使用rails 3.1.3和jquery运行.
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
Run Code Online (Sandbox Code Playgroud)
该协会是一个象征,必须是.\"...\"这里的目的是#{..}什么?删除它会导致错误.
目的是转义出现在相同类型的引号字符内的引号字符,以确保字符串不以此字符结尾
适当使用的示例
"This is a \"quote\""
=> This is a "quote"
'This is a \'quote\''
=> This is a 'quote'
"This is a 'quote'"
=> This is a 'quote'
'This is a "quote"'
=> This is a "quote"
Run Code Online (Sandbox Code Playgroud)