· 1 min read
Screencast: Formtastic Teil 2
Download (19.7 MB, 9:11) Alternativer Download für iPod & Apple TV(12.6 MB, 9:11)
Resourcen:
- Episode 184: Formtastic Part 1
- Formtastic
- Formtastic RDoc
- Validation Reflection
- Full episode source code
Quellcode:
[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 [/bash] [ruby] # 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 :animial belongs_to :problem end
# config/initializers/formtastic_config.rb Formtastic::SemanticFormBuilder.inline_errors = :none [/ruby]
[javascript] /* formtastic_changes.css */ form.formtastic fieldset ol li p.inline-hints { font-style: italic; font-size: 11px; } [/javascript]
[html]
<% 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 %> \[/html\]