0
選擇我有clients
和invoices
, 應用程序當用戶創建他們需要把它關聯到client
它,我在我的form
一個collection input field
成功地做一個invoice
。我添加了'include_blank'來處理沒有任何客戶端的新用戶註冊的情況。這是我遇到麻煩的地方。輸入字段的收藏價值沒有得到關於編輯
我加:include_blank => "Select Client"
後,我的編輯形式停止選擇相應的客戶端,而是現在默認「選擇客戶端」
我的形式輸入
<%= f.input_field :client_id, :collection => current_user.clients.order(created_at: :desc), selected: params[:client_id], :label_method => :client_name, :value_method => :id, :include_blank => "Select Client", class: "form-control" %>
爲什麼:include_blank
在編輯形式表示將client_id
是否存在?我如何獲得關聯的client_id
以顯示?
我的形式
<%= simple_form_for [@client, @invoice] do |f| %>
<div class="field">
<% if @invoice.errors.any? %>
<ul>
<% @invoice.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>
<...>
<div class="row invoice-info">
<div class="col-xs-2 invoice-col">
Select Client
<%= f.input_field :client_id, :collection => current_user.clients.order(created_at: :desc), selected: params[:client_id], :label_method => :client_name, :value_method => :id, :include_blank => "Select Client", class: "form-control" %>
</div>
<div class="col-xs-2 invoice-col">
<%= link_to 'New Client', new_client_path(@client), class: "btn btn-danger" %>
</div>
</div>
很高興看到更多的ERB圍繞這條線。特別是'form_for'的外觀。雖然,這可能與'params [:client_id]'有關。在處理這個ERB時,你確定'params'散列有那個鍵嗎? –
是的,如果我刪除':include_blank =>「選擇客戶端」'一切都按預期工作。正確的客戶端出現在輸入字段中 – Aloalo
當你通過':prompt =>「Select Client」'而不是'include_blank'時,你會看到相同的結果嗎? –