ruby 无法将 nil 转换为 String
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8852264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can't convert nil into String
提问by user1147884
I don't know Ruby but I'm having this problem with my application. Somebody can help me?
我不知道 Ruby,但我的应用程序遇到了这个问题。有人可以帮助我吗?
ActionView::TemplateError (can't convert nil into String) on line #7
第 7 行的 ActionView::TemplateError(无法将 nil 转换为 String)
4: <h3><span><%= opinion.opinion %></span>: <%= opinion.keywords.sort.join(' / ').upcase %> </h3>
5: <div class="original">Estado original: <span class="pos"><%= opinion.opinion %></span></div>
6: <%#= highlight(simple_format(highlight_ontology(opinion)), opinion.keywords, :highlighter => '<em></em>') %>
7: <%= simple_format(highlight_ontology(opinion)) %>
8: <%= button_to_remote('POSITIVO', {:url => url_for(opinion), :method => :put, :with => '"opinion[opinion]=POSITIVE"' }, :class => :positive) %>
9: <%= button_to_remote('OBJETIVO', {:url => url_for(opinion), :method => :put, :with => '"opinion[opinion]=OBJECTIVE"' }, :class => :objective) %>
10: <%= button_to_remote('NEGATIVO', {:url => url_for(opinion), :method => :put, :with => '"opinion[opinion]=NEGATIVE"' }, :class => :negative) %>
Thanks!
谢谢!
回答by Emyr
Using (variable || "")instead of just the variable should make it default to an empty string.
使用(variable || "")而不是仅使用变量应该使其默认为空字符串。
This technique is called null(or nil) coalescing.
这种技术称为空(或零)合并。

