Ruby on rails Rails—构建一个助手,用<;WBR>;
我的应用程序发送电子邮件。如果有一个很长的单词或一个很长的URL,它会破坏电子邮件浏览体验,因为它不会让iphone的zoom广告收紧 这是我到目前为止的想法,但它不起作用,是吗 助手Ruby on rails Rails—构建一个助手,用<;WBR>;,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我的应用程序发送电子邮件。如果有一个很长的单词或一个很长的URL,它会破坏电子邮件浏览体验,因为它不会让iphone的zoom广告收紧 这是我到目前为止的想法,但它不起作用,是吗 助手 def html_format(string, max_width=12) text = string.gsub("\n", '<br />').html_safe.strip (text.length < max_width) ? text : text.scan(/.{1,
def html_format(string, max_width=12)
text = string.gsub("\n", '<br />').html_safe.strip
(text.length < max_width) ?
text :
text.scan(/.{1,#{max_width}}/).join("<wbr>")
return text
end
def html_格式(字符串,最大宽度=12)
text=string.gsub(“\n”,“
”).html\u safe.strip
(文本长度<最大宽度)?
正文:
text.scan(/.{1,#{max_width}}}/).join(“”)
返回文本
结束
查看
<%= html_format(@comment.content) %>
展示了如何用正则表达式包装长单词。这里有一个似乎很适合用拆分长字符串的方法:
def split_str(str,len=10)
片段=/。{{len}/
str.split(/(\s+/).map!{字|
(/\s/==word)?word:word.gsub(片段“\0”)
}.加入
结束
您可以使用simple\u format()
而不是string.gsub(“\n”,”
')
。谢谢,我试过了,但没用。不会改变输出。你让它起作用了吗?或者只是通过谷歌搜索找到?我让它工作了。
def split_str(str, len = 10)
fragment = /.{#{len}}/
str.split(/(\s+)/).map! { |word|
(/\s/ === word) ? word : word.gsub(fragment, '\0<wbr />')
}.join
end