Ruby-on-rails Rails - simple_form,在新的集合中包含一个命名的空白对象并编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11393933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails - simple_form, include a named blank object in a collection on new and edit
提问by bdx
I have a simple_form which I am trying to get to always include a blank item in it, as a 'nil' value in that field has a special meaning in this database. In order to make it more obvious for end users, I also want to title it with something along the lines of "(select if none)".
我有一个 simple_form,我试图让它始终包含一个空白项目,因为该字段中的“nil”值在此数据库中具有特殊含义。为了使最终用户更明显,我还想用“(如果没有,则选择)”这样的内容来命名它。
I'm currently doing this, but it only inserts the 'blank' item when creating a new object, not when editing one.
我目前正在这样做,但它只在创建新对象时插入“空白”项,而不是在编辑对象时插入。
# _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
回答by deefour
You want to use :include_blank, not :prompt
你想使用:include_blank,而不是:prompt
<%= f.association :parent, :collection => @parents, :include_blank => "(select if none)" %>

