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
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
85
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
70
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
60
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
85
GDG Meetup - Carreiras em T.I.
jackson_pires
0
91
20 minutos insanos de TDD e Ruby
jackson_pires
0
110
Ruby on Rails para Iniciantes - Aula 46
jackson_pires
0
170
Ruby on Rails para Iniciantes - Aula 47
jackson_pires
0
84
Ruby on Rails para Iniciantes - Aula 49
jackson_pires
0
170
Other Decks in Programming
See All in Programming
t *testing.T は どこからやってくるの?
otakakot
1
930
cloudnative conference 2026 flyle
azihsoyn
0
170
サプライチェーン攻撃対策「層を重ねて落ちない壁」を10日間で組み上げた話 #TechLeadConf2026
kashewnuts
1
250
Kingdom of the Machine
yui_knk
2
1.5k
PHPer、Cloudflare に引っ越す
suguruooki
1
160
AIを導入する前にやるべきこと
negima
2
350
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
3
340
開発とはなにか、Essenceカーネルで見えるもの
ukin0k0
0
140
AWSはOSSをどのように 考えているのか?
akihisaikeda
0
110
Explore CoroutineScope
tomoeng11
0
180
AI-DLC Deep Dive
yuukiyo
9
5.7k
AI時代になぜ書くのか
mutsumix
0
380
Featured
See All Featured
The Curious Case for Waylosing
cassininazir
0
340
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Visualization
eitanlees
150
17k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.8k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
210
Embracing the Ebb and Flow
colly
88
5k
The Spectacular Lies of Maps
axbom
PRO
1
740
Art, The Web, and Tiny UX
lynnandtonic
304
21k
Utilizing Notion as your number one productivity tool
mfonobong
4
300
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
180
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
110
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
140
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!