Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

ruby - Rails simple_format not handling some strings

I'm trying to use Rails' simple_format() method, but it isn't working as examples online seem to indicate: It works on a double quoted string, but not a single quoted string, and it also doesn't work on text fields in models returned from my DB.

simple_format "Hello

World"
# Hello
# World

simple_format 'Hello

World'
# Hello

World

simple_format @model.hello_world
# Hello

World

simple_format "#{@model.hello_world}"
# Hello

World

Why does the method treat these string differently? I haven't seen anything in the examples or docs stating that this behavior is expected, specifically with strings provided from model fields. How can I display my dynamic content with formatting?

question from:https://stackoverflow.com/questions/65951236/rails-simple-format-not-handling-some-strings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ruby treats single quotes and double quotes differently. Documentation on Strings

In single quoted strings only backslashes and apostrophes are escaped (supported). This is why "#{variable}" works in Rails but '#{variable}' doesn't. If you need to escape something (variable, newline, etc.), use double quotes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...