开发者

Changing error display format in Rails ActionView Helpers

I'm trying to modify the default behaviour of showing errors before the form to show them next to the fields instead.

I'm using this to achieve that:

  ActionView::B开发者_如何转开发ase.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

However, for some reason, the result html is coded with entities, so it doesn't get displayed:

<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>

How can I avoid the result being html_entitied?


It's because your string is not safe. You need call html_safe after the string generate

  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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜