Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby on Rails para Iniciantes - Aula 48
Search
Jackson Pires
March 16, 2016
Programming
390
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
Transcript
27 slides
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!
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
100
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
83
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
74
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
93
GDG Meetup - Carreiras em T.I.
jackson_pires
0
100
20 minutos insanos de TDD e Ruby
jackson_pires
0
130
Ruby on Rails para Iniciantes - Aula 46
jackson_pires
0
180
Ruby on Rails para Iniciantes - Aula 47
jackson_pires
0
96
Ruby on Rails para Iniciantes - Aula 49
jackson_pires
0
180
Other Decks in Programming
See All in Programming
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
370
React Native에 현대적인 빌드 도구 더하기: Rollipop 구현기
leegeunhyeok
0
180
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
2k
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
320
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
170
クラウドエージェントは、 何がそんなに便利なのか / what makes cloud agents so useful
rkaga
5
830
Getting Started with Vibe Coding
miccheng
0
200
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
2
250
Event is what happens
moro
2
4.2k
Vapor Mode でアクセシビリティは 壊れないか検証した話
fuqda
1
340
LLM 101
dalinaum
0
480
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
310
Featured
See All Featured
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
400
The Cost Of JavaScript in 2023
addyosmani
56
10k
The Curse of the Amulet
leimatthew05
4
17k
A better future with KSS
kneath
240
18k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
560
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Producing Creativity
orderedlist
PRO
348
41k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
350
Odyssey Design
rkendrick25
PRO
3
940
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.6k