7
我正在使用以下帮助程序函数,但它似乎将我的JavaScript语句中的所有特殊字符转换为HTML实体,使其无法使用并损坏。有什么建议么?防止rails将特殊字符转换为HTML实体
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 + "_fields", :f => builder)
end
link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
end
上面产生像这样的链接(注意转换次数$amp;
- "
等:
<a href="#" onclick="add_fields(this, &quot;skills&quot;, &quot;&lt;label for=\&quot;user_skills_attributes_new_skills_name\&quot;&gt;Skill&lt;\/label&gt;\n&lt;input data-autocomplete=\&quot;/users/autocomplete_skills_vocab_name\&quot; id=\&quot;user_skills_attributes_new_skills_name\&quot; name=\&quot;user[skills_attributes][new_skills][name]\&quot; size=\&quot;30\&quot; type=\&quot;text\&quot; /&gt;&lt;br /&gt;\n&lt;input id=\&quot;user_skills_attributes_new_skills__destroy\&quot; name=\&quot;user[skills_attributes][new_skills][_destroy]\&quot; type=\&quot;hidden\&quot; value=\&quot;false\&quot; /&gt;&lt;a href=\&quot;#\&quot; onclick=\&quot;remove_fields(this); return false;\&quot;&gt;remove&lt;\/a&gt;&quot;); return false;">Add a Skill</a>
EDIT/
想通了 - 对于导轨3除去H2S()
你复制它不知道它是如何工作的?提示:h()是做什么的? – 2011-04-21 12:21:02
非常诚实,我使用的是Railscasts插曲中的代码:http://railscasts.com/episodes/197-nested-model-form-part-2我删除了h(),现在它可以工作吗?我很困惑,为什么它被用在Railscast中...... – stewart715 2011-04-21 12:21:59
Bleh,我认为这是一个Rails 3问题 - 感谢Mark的那个快速修复.. – stewart715 2011-04-21 12:24:35