Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - simple_form, include a named blank object in a collection on new and edit

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)".

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 
like image 431
bdx Avatar asked Jul 09 '12 11:07

bdx


1 Answers

You want to use :include_blank, not :prompt

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

The documentation

like image 189
deefour Avatar answered Sep 26 '22 08:09

deefour