'이전'과 '이후'두 필드가있는 Content라는 모델이 있습니다.
레코드를 만들기 전에 'before'필드에서 몇 가지 전처리를 수행하고 'after'에 저장합니다. 여기서 문제 없습니다.
문제는 쇼 페이지를로드 할 때 'before'의 내용이 불완전하다는 것입니다. 2 개 이상있는 경우 마침표 후 공백을 잘라냅니다 ... 예
"this is. some text. with many spaces. after the periods."
보기는 '이전'이 다음과 같이 보이는 것을 보여줍니다.
"this is. some text. with many spaces. after the periods."
.json 버전을 보면 'before'필드에 원래 형식이 포함되어 있습니다. 왜 rails 뷰 (json이 아님)에서 'before'가 "트리밍"된 것으로 표시됩니까?
이것은 내 _content.html.erb입니다.
<li><%= content.before %></li>
<li><%= content.after %></li>
이것은 내 show.js.erb입니다.
$("#main").html("<%= escape_javascript(render @content) %>");
이것은 contents_controller.rb의 쇼 정의입니다.
respond_to :html, :json
def show
@content = Content.find(params[:id])
respond_with(@content)
end
이것은 내 모델 content.rb의 전처리입니다.
class Content < ActiveRecord::Base
before_validation :format_params
def format_params
rm_spaces = /\.\s{2,}/ # regex to rm extra spaces
new_format = '. ' # only one space after period
self.after = before.gsub(rm_spaces, new_format)
end
end
그리고 여기 제가 의미하는 바에 대한 스크린 샷이 있습니다.
편집 Swards 권장 사항을 시도했지만 작동하지 않았기 때문에 이것이 실행되는 코드라고 생각합니다.
이것은 내 contents_helper.rb의 강조 기능 코드입니다.
def highlight_changes(text)
highlighter = '<span style= "background: yellow">\1</span>'
matcher = /(\.\s{2,})/
text.gsub(matcher, highlighter).html_safe
end
이것은 show.html.rb의 코드입니다.
<p>
<strong>Before:</strong>
<!-- call the helper method to highlight changes made -->
<%= highlight_changes @content.before %>
</p>
.before
before li에 클래스를 넣고 이것을 css에 추가하여 입력 된대로 볼 수 있습니다.
<li class="before"><%= content.before %></li>
<li><%= content.after %></li>
CSS에서
.before {
white-space: pre;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다