我有clients
和invoices
的应用,
当用户创建invoice
时,他们需要将其与我client
中collection input field
成功完成的form
相关联。我添加了'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 :(得分:0)
好的,我删除了selected: params[:client_id]
,现在它按预期正常运行:include_blank => "Select Client"
selected: params[:client_id]
正在返回nil
,表单构建器似乎能够在:client_id
被删除后解释selected: params[:client_id]
。