#185 Formtastic Part 2
Learn about some of the more advanced functionality of Formtastic including handling many-to-many associations, required fields, and styling.
- Download:
- source codeProject Files in Zip (111 KB)
- mp4Full Size H.264 Video (15.3 MB)
- m4vSmaller H.264 Video (10 MB)
- webmFull Size VP8 Video (27.8 MB)
- ogvFull Size Theora Video (20.8 MB)
Resources
- Episode 184: Formtastic Part 1
- Formtastic
- Formtastic RDoc
- Validation Reflection
- Full episode source code
bash
script/generate nifty_scaffold problem name:string rake db:migrate script/generate nifty_scaffold symptom animal_id:integer problem_id:integer --skip-controller script/plugin install git://github.com/redinger/validation_reflection.git
models/animal.rb
class Animal < ActiveRecord::Base attr_accessible :name, :category_id, :born_on, :female, :problem_ids belongs_to :category has_many :symptoms has_many :problems, :through => :symptoms validates_presence_of :name, :born_on end
models/problem.rb
class Problem < ActiveRecord::Base attr_accessible :name has_many :symptoms has_many :animals, :through => :symptoms end
models/symptom.rb
class Symptom < ActiveRecord::Base attr_accessible :animal_id, :problem_id belongs_to :animal belongs_to :problem end
config/initializers/formtastic_config.rb
Formtastic::SemanticFormBuilder.inline_errors = :none
formtastic_changes.css
form.formtastic fieldset ol li p.inline-hints { font-style: italic; font-size: 11px; }
views/animals/_form.html.erb
<% semantic_form_for @animal do |f| %> <%= f.error_messages %> <% f.inputs do %> <%= f.input :name, :hint => "Use the owner's name if none is provided" %> <%= f.input :born_on, :start_year => 1900 %> <%= f.input :category, :include_blank => false %> <%= f.input :female, :as => :radio, :label => "Gender", :collection => [["Male", false], ["Female", true]] %> <%= f.input :problems, :as => :check_boxes, :required => false %> <% end %> <%= f.buttons %> <% end %>


