簡體   English   中英

如何在Ruby on Rails中的html erb文件中有條件地顯示div

[英]How to conditionally display a div inside a html erb file in Ruby on Rails

我想顯示一個基於ID是否存在的按鈕。

_form.html.erb

<%= @customer.id? %>
    <div class="pull-right">
        <%= button_to 'Kundenrofil', customer_path(@customer), method: :get %>
   </div>
<%= end %>

我在表格中使用它來結交新客戶並編輯客戶。

如果我編輯客戶,一切都會很好,但是如果我創建新用戶,則會給我一個錯誤,因為

id=nil

我如何在_form.html.erb中做到這一點

<% unless @customer.id.nil? %>
    <div class="pull-right">
        <%= button_to 'Kundenrofil', customer_path(@customer), method: :get %>
   </div>
<% end %>

您必須使用if/unless表達式。 那么更改@customer.id? unless @customer.id.nil? 還要注意<%而不是<%= <%=用於我們要在頁面上呈現的內容。 <%用於執行括號內的紅寶石代碼。 <%= end %>更改為<% end %>

引用this以了解差異。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM