2016-04-01 24 views
0

選擇我有clientsinvoices, 應用程序當用戶創建他們需要把它關聯到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> 
+0

很高興看到更多的ERB圍繞這條線。特別是'form_for'的外觀。雖然,這可能與'params [:client_id]'有關。在處理這個ERB時,你確定'params'散列有那個鍵嗎? –

+0

是的,如果我刪除':include_blank =>「選擇客戶端」'一切都按預期工作。正確的客戶端出現在輸入字段中 – Aloalo

+0

當你通過':prompt =>「Select Client」'而不是'include_blank'時,你會看到相同的結果嗎? –

回答

0

好吧,我刪除selected: params[:client_id]現在它正在按預期工作與:include_blank => "Select Client"

selected: params[:client_id]返航nil,似乎形式建造者是能夠解釋:client_id一次selected: params[:client_id]取出的。