2012-07-09 14 views
23

我有一个simple_form,我试图去总是在其中包含一个空白项目,因为该字段中的'nil'值有一个特殊在这个数据库中的含义 为了让它对最终用户更为明显,我也想用“(如果没有选择)”这样的句子来标题。Rails - simple_form,在新的和编辑的集合中包含一个已命名的空白对象

我目前正在这样做,但它只在创建新对象时插入'空白'项,而不是在编辑时插入。

# _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 

回答

53

你想用:include_blank,不:prompt

<%= f.association :parent, :collection => @parents, :include_blank => "(select if none)" %> 

The documentation

+0

感谢您的。我已经阅读了足够多的文档,显然一直在跳过这一点。 – bdx 2012-07-09 11:22:31

+0

注意自我: '''include_blank :::所有''' 将不起作用。 '''include_blank:'All'''' does ... – kwerle 2017-10-24 16:50:14

相关问题