我將三元運算子用于單行條件,帶有 erb 列印標簽 <%= %>
<p>Status:
<%= notification.read? ? "Read" : link_to "Mark as Read", "/" %>
</p>
我需要在錯誤條件下標記為已讀鏈接,在上述場景中得到語法模板錯誤,這里的通知是通知模型的一個物件。
我想要輸出為-
mark as read
標記為已讀將是鏈接。
謝謝!
uj5u.com熱心網友回復:
不要使用三元:
<% if notification.read? %>
Read
<% else %>
<%= link_to 'Mark as Read', '/' %>
<% end %>
或者在link_to方法呼叫中使用括號:
<%= notification.read? ? 'Read' : link_to('Mark as Read', '/') %>
請記住,里面的任何東西<%= ... %>都只是 Ruby,它link_to只是一個 Ruby 方法,就像其他任何方法一樣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/524504.html
標籤:轨道上的红宝石红宝石erb链接到ruby-on-rails-7
上一篇:Ruby條件陳述句最佳實踐
