我有一个 simple_form,我试图在其中始终包含一个空白项,因为该字段中的“nil”值在此数据库中具有特殊含义。为了让最终用户更清楚,我还想用类似“(如果没有则选择)”的方式来命名它。
我目前正在这样做,但它只在创建新对象时插入“空白”项目,而不是在编辑时插入。
# _child_form.html.erb
<%= simple_form_for @child do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.association :parent, :collection => @parents, :prompt => "(select if none)" %>
<%= f.button.submit %>
<% end %>
.
# child_controller.rb
def new
@child = Child.new
@parents = Parent.all
end
def edit
@child = Child.find(params[:id])
@parents = Parent.all
end