[英]syntax error, unexpected unary+, expecting keyword_end
使用以下代碼獲取此一元+錯誤,請查看先前的解決方案,他們認為某些情況下缺少結尾 ,但事實並非如此。
請幫忙
<% if part.children.exists? %>
<% @link_text += " <b>|</b> " %>
<% part.children.each do |part_child| %>
<% if part_child.display_type == "radio" || part_child.display_type == "dropdown" || part_child.display_type == "checkbox" %>
<% if part_child.display_type == "checkbox" %>
<% @checkbox_options = "" %>
<% part_child.options.each do |o| %>
<% if o.is_default? %>
<% @co = "<span id = 'nav_option_id_" + o.id +"'>" + o.name +"</span>" %>
<% @checkbox_options += @co %>
<% end %>
<% end %>
<% @link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_" + part_child.id.to_s + "'>" "</span>" %>
<% else %>
<% @link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_" + part_child.id.to_s + "'>" + part_child.options.detect{|o| o.is_default?}.try(:name).to_s + "</span>" + " <b>|</b> " %>
<% end %>
<% end %>
<% end %>
<% end %>
我認為這是問題所在...
<% @link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_" + part_child.id.to_s + "'>" "</span>" %>
最后兩個元素之間缺少“ +”。
<% @link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_" + part_child.id.to_s + "'>" + "</span>" %>
正如BrioSatse在評論中所說,插值會更好。
你可以試試這個嗎?
<% if part.children.exists? %>
<% @link_text += " <b>|</b> " %>
<% part.children.each do |part_child| %>
<% if %w(radio dropdown checkbox).include?(part_child.display_type) %>
<% if part_child.display_type.eql?("checkbox") %>
<% @checkbox_options = "" %>
<% part_child.options.each do |o| %>
<% if o.is_default? %>
<% @co = "<span id = 'nav_option_id_#{o.id}'>#{o.name}</span>" %>
<% @checkbox_options += @co %>
<% end %>
<% end %>
<% @link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_#{part_child.id.to_s}'></span>" %>
<% else %>
<% @link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_#{part_child.id.to_s}'>#{part_child.options.detect{|o| o.is_default?}.try(:name).to_s}</span>"+" <b>|</b> " %>
<% end %>
<% end %>
<% end %>
<% end %>
我稀疏的問題在於:
<% @link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_" + part_child.id.to_s + "'>" "</span>" %>
最后一個</span>
附近的引號沒有意義。 我還重寫了您的ERB輸出,以使其更具可讀性,並且更短。 我個人是使用#{}表示法將字符串與變量合並在一起的愛好者。
這條線
<% @link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_" + part_child.id.to_s + "'>" "</span>" %>
在</span>
前缺少+
。
這突出顯示了為此使用字符串連接的問題-這是一個丑陋,令人困惑的混亂。 這是編寫上面代碼的一種更好(且不間斷)的方法:
<% @link_text = += "<b>#{part_child.name}</b> : <span id='part_child_#{part_child.id.to_s}'></span>" %>
編輯:此外,如果您只是在視圖中執行紅寶石代碼的加載,而不實際渲染任何內容,則可以將全部包裝在單個erb標簽中,以使其更具可讀性。 例如
<%
if part.children.exists?
@link_text += " <b>|</b> "
part.children.each do |part_child|
if %w(radio dropdown checkbox).include?(part_child.display_type)
if part_child.display_type.eql?("checkbox")
@checkbox_options = ""
part_child.options.each do |o|
if o.is_default?
@co = "<span id = 'nav_option_id_#{o.id}'>#{o.name}</span>"
@checkbox_options += @co
end
end
@link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_#{part_child.id.to_s}'></span>"
else
@link_text = @link_text + "<b>#{part_child.name}</b> : <span id='part_child_#{part_child.id.to_s}'>#{part_child.options.detect{|o| o.is_default?}.try(:name).to_s}</span>"+" <b>|</b> "
end
end
end
end
%>
即使執行了此操作,您的代碼仍然看起來像是一種過於復雜的實現目標的方法。 像這樣構建大量的html字符串並不是一件容易的事。 您應該單獨呈現html塊,並在其周圍包裹邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.