Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby on Rails para Iniciantes - Aula 48
Search
Jackson Pires
March 16, 2016
Programming
380
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby on Rails para Iniciantes - Aula 48
Ruby on Rails para Iniciantes - Aula 48
Has Many Through + Cocoon gem
Jackson Pires
March 16, 2016
More Decks by Jackson Pires
See All by Jackson Pires
Como usar uma box Vagrant com a Cloud9 IDE para desenvolver com Elixir ou Phoenix?
jackson_pires
0
93
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
78
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
67
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
87
GDG Meetup - Carreiras em T.I.
jackson_pires
0
100
20 minutos insanos de TDD e Ruby
jackson_pires
0
120
Ruby on Rails para Iniciantes - Aula 46
jackson_pires
0
180
Ruby on Rails para Iniciantes - Aula 47
jackson_pires
0
87
Ruby on Rails para Iniciantes - Aula 49
jackson_pires
0
180
Other Decks in Programming
See All in Programming
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
800
VibeCodingからAgenticWorkflowへ
starfish719
0
600
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
230
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
1k
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
120
Foundation Models frameworkで画像分析
ryodeveloper
1
620
Cloudflare is Agents
chimame
0
170
バグを直したら useEffect が消えた
colorful12
2
570
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
510
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
560
FDEが実現するAI駆動経営の現在地
gonta
2
290
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
420
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
430
Done Done
chrislema
186
16k
Building AI with AI
inesmontani
PRO
1
1.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Odyssey Design
rkendrick25
PRO
2
760
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
240
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
490
Producing Creativity
orderedlist
PRO
348
40k
Accessibility Awareness
sabderemane
1
180
The SEO identity crisis: Don't let AI make you average
varn
0
530
Transcript
Ruby on Rails Para iniciantes - 4a Temporada ;-)
http://videosdeti.com.br Subscribe!
Aula 48 Has Many Through + Cocoon gem
Ruby on Rails Has Many Through http://guides.rubyonrails.org/association_basics.html#the- has-many-through-association
Ruby on Rails
Ruby on Rails
Ruby on Rails
Ruby on Rails Scaffolds rails generate scaffold Physician name:string rails
generate scaffold Appointment physician:references patient:references appointment_date:datetime rails generate scaffold Patient name:string
Ruby on Rails class Physician < ActiveRecord::Base has_many :appointments has_many
:patients, through: :appointments end class Appointment < ActiveRecord::Base belongs_to :physician belongs_to :patient end
Ruby on Rails
Ruby on Rails class Patient < ActiveRecord::Base has_many :appointments has_many
:physicians, through: :appointments end
Ruby on Rails
Ruby on Rails Nested Attributes
Ruby on Rails class Physician < ActiveRecord::Base has_many :appointments has_many
:patients, through: :appointments accepts_nested_attributes_for :appointments, reject_if: : all_blank, allow_destroy: true end
Ruby on Rails
Ruby on Rails class Appointment < ActiveRecord::Base belongs_to :physician belongs_to
:patient accepts_nested_attributes_for :patient, reject_if: : all_blank, allow_destroy: true end
Ruby on Rails
Ruby on Rails Strong Parameters def physician_params params.require(:physician).permit(:name, : appointments_attributes
=> [:id, :appointment_date, : physician_id, :patient_id, :_destroy, :patient_attributes => [: id, :name]]) end
Ruby on Rails Cocoon https://github.com/nathanvda/cocoon
Ruby on Rails Gemfile gem "cocoon"
Ruby on Rails application.js //= require cocoon
Ruby on Rails Helpers link_to_add_association This function adds a link
to your markup that, when clicked, dynamically adds a new partial form for the given association.
Ruby on Rails Helpers link_to_remove_association This function will add a
link to your markup that, when clicked, dynamically removes the surrounding partial form. This should be placed inside the partial _<association-object-singular>_fields.
Ruby on Rails Formulário Physician ... <%= link_to_add_association 'add appointment',
f, :appointments, 'data-association-insertion-node' => "#appointments-patient ol", 'data-association-insertion-method' => "append", :wrap_object => Proc. new {|appointment| appointment.build_patient; appointment } %> <hr/>
Ruby on Rails Formulário Physician <fieldset id="appointments-patient"> <ol> <%= f.fields_for
:appointments do |appointment| %> <%= render partial: "appointment_fields", locals: { f: appointment } %> <% end %> </ol> <fieldset/>
Ruby on Rails _appointment_fields.html.erb <li class="control-group nested-fields"> <div class="controls"> <%=
f.datetime_select :appointment_date %>> <%= f.fields_for :patient do |appointment_patient| %> <%= appointment_patient.text_field :name %> <% end %> <%= link_to_remove_association "remove", f %> </div> </li>
Ruby on Rails Obrigado!