2010-10-22 19 views
0

我试图修改在窗体之前显示错误的默认行为,而不是在字段旁边显示它们。在Rails中更改错误显示格式ActionView帮手

我用这个来实现这一目标:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| 
     if instance.error_message.kind_of?(Array) 
     %(#{html_tag}<span class="validation-error">&nbsp; 
     #{instance.error_message.join(',')}</span>) 
     else %(#{html_tag}<span class="validation-error">&nbsp; 
     #{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"> 
    &lt;label class=&quot;label&quot; for=&quot;user_street&quot;&gt;Address&lt;/label&gt;&lt;span class=&quot;validation-error&quot;&gt;&amp;nbsp; 
     translation missing: pl, activerecord, errors, models, user, attributes, street, blank&lt;/span&gt; 

    &lt;input class=&quot;text_field&quot; id=&quot;user_street&quot; name=&quot;user[street]&quot; size=&quot;30&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;&lt;span class=&quot;validation-error&quot;&gt;&amp;nbsp; 
     translation missing: pl, activerecord, errors, models, user, attributes, street, blank&lt;/span&gt; 

    <span class="description"> &nbsp;</span> 
    </div> 

如何我能够避免,结果被html_entitied?

回答

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">&nbsp; 
     #{instance.error_message.join(',')}</span>).html_safe 
     else %(#{html_tag}<span class="validation-error">&nbsp; 
     #{instance.error_message}</span>).html_safe 
     end 
    end 
相关问题