【发布时间】:2015-08-24 22:34:10
【问题描述】:
我有一个名为 Content 的模型,其中包含两个字段“之前”和“之后”。
在创建记录之前,我对字段“之前”进行了一些预处理,并将其存储在“之后”中。这里没问题。
问题是当我加载显示页面时,“之前”中的内容显示不完整。如果有 2 个或更多,我会在一段时间后修剪空白... 例如
"this is. some text. with many spaces. after the periods."
视图显示“之前”看起来像这样:
"this is. some text. with many spaces. after the periods."
如果我查看 .json 版本,“之前”字段包含原始格式 - 为什么 Rails 视图(非 json)将“之前”显示为“修剪”?
这是我的 _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
这是我模型内容中的预处理.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>
【问题讨论】:
标签: ruby-on-rails ruby ajax json