Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Formtastic: Select-Boxes without primary blank field

It's about Rails and Formtastic.

How can I add a select box with formtastic without an initial/primary blank field? So that the initially selected item is the first item with content.

like image 359
Joern Akkermann Avatar asked May 26 '10 10:05

Joern Akkermann


2 Answers

Have you tried :include_blank => false ?

According to this (line 718) http://github.com/justinfrench/formtastic/blob/master/lib/formtastic.rb that should work.

like image 144
tsdbrown Avatar answered Oct 15 '22 11:10

tsdbrown


You resolve this one of two ways:

First option: In each select box, specify if there should be a blank line or not. Options are:

<%= f.input :author, :as => :select, :include_blank => false %>
<%= f.input :author, :as => :select, :include_blank => true %>
<%= f.input :author, :as => :select, :include_blank => "No author" %>

The last version displays "No Author" as the display in the drop down, but submits the value as blank.

Second Option: Set the default in the config/initializers/formtastic.rb.

# Should select fields have a blank option/prompt by default?
# Defaults to true.
Formtastic::FormBuilder.include_blank_for_select_by_default = false

By default, this is set to true and all your drop downs will have blank options in them. Set it to false, and by default they all won't.

like image 24
Kevin Bedell Avatar answered Oct 15 '22 11:10

Kevin Bedell