Ruby can';t将nil转换为字符串
我不知道Ruby,但我的应用程序有这个问题。有人能帮我吗 第7行的ActionView::TemplateError(无法将nil转换为字符串)Ruby can';t将nil转换为字符串,ruby,string,null,Ruby,String,Null,我不知道Ruby,但我的应用程序有这个问题。有人能帮我吗 第7行的ActionView::TemplateError(无法将nil转换为字符串) 4:: 5:Estado原件: 6: '\1') %> 7: 8:url_表示(意见),:方法=>:put,:with=>'“意见[意见]=积极”},:类=>:POSITIVE)%> 9:url_表示(意见),:方法=>:put,:with=>'“意见[意见]=OBJECTIVE”},:类=>:OBJECTIVE)%> 10:url_表示(意见
4::
5:Estado原件:
6: '\1') %>
7:
8:url_表示(意见),:方法=>:put,:with=>'“意见[意见]=积极”},:类=>:POSITIVE)%>
9:url_表示(意见),:方法=>:put,:with=>'“意见[意见]=OBJECTIVE”},:类=>:OBJECTIVE)%>
10:url_表示(意见),:方法=>:put,:with=>'“意见[意见]=负面”},:类=>:NEGATIVE)%>
谢谢 使用(变量| |“”)
而不仅仅是该变量应该使其默认为空字符串
这种技术称为null(或nil)合并。根据变量的来源,在视图中使用它之前,您可能已经希望将其默认为“”。在你的例子中,这可能在highlight_ontology.Query中:错误是“无法将nil转换为字符串”。你如何/为什么可以将相同的变量强制转换为字符串?@adamwong246我会提出相反的问题。既然
NilClass
将定义为
,为什么它不能转换为字符串?答案是因为它没有将定义为_str
。这就是为什么“#{nil}”起作用并且“#{”+nil}”导致上述errorUpvote变量.to_
<代码>“abc”。要获取“abc”
,无。要获取“
”。
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>\1</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) %>