[英]Prevent rails from converting special characters to HTML entities
我正在使用以下帮助函数,但它似乎将我的JavaScript语句中的所有特殊字符转换为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>
编辑/
想出来 - 对于Rails 3删除h()
在Rails 2中,默认输出未被转义。 h()方法执行此操作。 在Rails 2视图中,您经常会看到以下内容:
<%=h @object.field %>
但是,在Rails 3中,输出现在默认是转义的。 您不再需要h()方法。 为了获得非转义输出,您必须使用原始方法。
有关更多信息,请访问: http : //railscasts.com/episodes/204-xss-protection-in-rails-3
因此,基本上在你的情况下,你正在查看Rails 2代码,并且需要删除h()来更新Rails 3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.