我正在尝试修改在表单之前显示错误的默认行为,而不是在字段旁边显示错误。
我正在用它来实现这个目标:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if instance.error_message.kind_of?(Array)
%(#{html_tag}<span class="validation-error">
#{instance.error_message.join(',')}</span>)
else %(#{html_tag}<span class="validation-error">
#{instance.error_message}</span>)
end
end
但是,出于某种原因,结果html使用实体进行编码,因此不会显示:
<div class="group">
<label class="label" for="user_city">City and Postcode</label>
<input class="text_field" id="user_city" name="user[city]" size="30" type="text" value="94-050 Łódź" />
<span class="description">np. 00-000 Łódź</span>
</div>
<div class="group">
<label class="label" for="user_street">Address</label><span class="validation-error">&nbsp;
translation missing: pl, activerecord, errors, models, user, attributes, street, blank</span>
<input class="text_field" id="user_street" name="user[street]" size="30" type="text" value="" /><span class="validation-error">&nbsp;
translation missing: pl, activerecord, errors, models, user, attributes, street, blank</span>
<span class="description"> </span>
</div>
如何避免结果为html_entitied?
答案 0 :(得分:0)
这是因为你的字符串不安全。你需要在字符串生成后调用html_safe
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if instance.error_message.kind_of?(Array)
%(#{html_tag}<span class="validation-error">
#{instance.error_message.join(',')}</span>).html_safe
else %(#{html_tag}<span class="validation-error">
#{instance.error_message}</span>).html_safe
end
end