유효성 검사 기능 내에서 사용자 정의 유효성 검사 오류 메시지에 html을 추가 할 수 있습니까? 예를 들어html로 사용자 정의 유효성 검사 오류 메시지
: 위를하는
class Product < ActiveRecord::Base
validates :legacy_code, :format => { :with => /\A[a-zA-Z]+\z/,
:message => "Only letters allowed <a href=\"www.example.com\"> Check here </a> " }
end
는 단순히 태그를 HTML로 해석 브라우저없이 문자열 리터럴을 제공합니다.
로케일을 사용해 보았지만 좀 더 복잡한 방법으로 보입니다. 나는 많은 웹 사이트를 탐색하고 또한 field_error_proc 메소드를 오버라이드하려고 시도했다. 예를 들어
:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
errors = Array(instance.error_message).join(',')
%(#{html_tag}<span class="validation-error"> #{errors}</span>).html_safe
end
위의 작품은 그러나 의도 한 것보다 오류 메시지 수의 두 배를 제공합니다.
여기의 도움을 주시면 감사하겠습니다.
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li>* <%= msg.html_safe %></li>
<% end %>
</ul>
</div>
<% end %>
raw
를 사용부분 오류 메시지에 .html_safe 사용하여를 해결 소독되지 않은 상태에서 오류 메시지에 무엇이든 넣으십시오. –