Skip to content

Instantly share code, notes, and snippets.

@azinazadi
Created November 17, 2011 11:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azinazadi/1372943 to your computer and use it in GitHub Desktop.
Save azinazadi/1372943 to your computer and use it in GitHub Desktop.
formtastic cheatsheet
more: https://github.com/justinfrench/formtastic
-> make form for all db columns
<%= semantic_form_for @user do |f| %>
<%= f.inputs %>
<%= f.buttons %>
<% end %>
-> select cols
<%= semantic_form_for @user do |f| %>
<%= f.inputs :title, :body, :section, :categories, :created_at %>
<%= f.buttons :commit %>
<% end %>
-> make form with two sections and hints for elements
<%= f.inputs "Basic", :id => "basic" do %>
<%= f.input :title %>
<%= f.input :body %>
<% end %>
<%= f.inputs :name => "Advanced Options", :id => "advanced" do %>
<%= f.input :slug, :label => "URL Title", :hint => "Created automatically if left blank", :required => false %>
<%= f.input :section, :as => :radio %>
<%= f.input :user, :label => "Author", :member_label => :full_name %>
<%= f.input :categories, :required => false %>
<%= f.input :created_at, :as => :string, :label => "Publication Date", :required => false %>
<% end %>
-> form for nested resources
<%= semantic_form_for [@author, @post] do |f| %>
...
-> multiple forms on the page: namespaces prevent from id clash
<%= semantic_form_for(@post, :namespace => 'cat_form') do |f| %>
-> custom HTML attributes for inputs: :input_html
f.input :body, :input_html => { :class => 'autogrow', :rows => 10, :cols => 20, :maxlength => 10 }
-> Customize the HTML attributes for the <li> wrapper
f.input :title, :wrapper_html => { :class => "important" }
f.input :title, :hint_class => 'custom-html-class', :error_class => 'custom-error-class'
-> options for custom elements
f.input :authors, :as => :check_boxes, :collection => User.find(:all, :order => "last_name ASC")
f.input :authors, :as => :check_boxes, :collection => current_user.company.users.active
f.input :authors, :as => :check_boxes, :collection => [@justin, @kate]
f.input :authors, :as => :check_boxes, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
f.input :author, :as => :select, :collection => Author.find(:all)
f.input :author, :as => :select, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
f.input :author, :as => :select, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
f.input :author, :as => :radio, :collection => User.find(:all)
f.input :author, :as => :radio, :collection => [@justin, @kate]
f.input :author, :as => :radio, :collection => { @justin.name => @justin.id, @kate.name => @kate.id }
f.input :author, :as => :radio, :collection => ["Justin", "Kate", "Amelia", "Gus", "Meg"]
f.input :admin, :as => :radio, :collection => ["Yes!", "No"]
--------------------
* available inputs
:select – a select menu. Default for ActiveRecord associations: belongs_to, has_many, and has_and_belongs_to_many.
:check_boxes – a set of check_box inputs. Alternative to :select for ActiveRecord-associations: has_many, and has_and_belongs_to_many.
:radio – a set of radio inputs. Alternative to :select for ActiveRecord-associations: belongs_to.
:time_zone – a select input. Default for column types: :string with name matching "time_zone".
:password – a password input. Default for column types: :string with name matching "password".
:text – a textarea. Default for column types: :text.
:date – a date select. Default for column types: :date.
:datetime – a date and time select. Default for column types: :datetime and :timestamp.
:time – a time select. Default for column types: :time.
:boolean – a checkbox. Default for column types: :boolean.
:string – a text field. Default for column types: :string.
:number – a text field (just like string). Default for column types: :integer, :float, and :decimal.
:file – a file field. Default for file-attachment attributes matching: paperclip or attachment_fu.
:country – a select menu of country names. Default for column types: :string with name "country" – requires a country_select plugin to be installed.
:email – a text field (just like string). Default for columns with name matching "email". New in HTML5. Works on some mobile browsers already.
:url – a text field (just like string). Default for columns with name matching "url". New in HTML5. Works on some mobile browsers already.
:phone – a text field (just like string). Default for columns with name matching "phone" or "fax". New in HTML5.
:search – a text field (just like string). Default for columns with name matching "search". New in HTML5. Works on Safari.
:hidden – a hidden field. Creates a hidden field (added for compatibility).
:range – a slider field.
--------
*create new inputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment