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
0
340
Ruby on Rails para Iniciantes - Aula 48
Ruby on Rails para Iniciantes - Aula 48
Has Many Through + Cocoon gem
Jackson Pires
March 16, 2016
Tweet
Share
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
64
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
50
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
49
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
73
GDG Meetup - Carreiras em T.I.
jackson_pires
0
61
20 minutos insanos de TDD e Ruby
jackson_pires
0
95
Ruby on Rails para Iniciantes - Aula 46
jackson_pires
0
160
Ruby on Rails para Iniciantes - Aula 47
jackson_pires
0
72
Ruby on Rails para Iniciantes - Aula 49
jackson_pires
0
150
Other Decks in Programming
See All in Programming
Git Lint
bkuhlmann
4
760
Node.js v22 で変わること
yosuke_furukawa
PRO
11
3.9k
try! Swift Tokyo 初参加報告LT
hinakko2
0
230
Documentation for users with AsciiDoc and Antora
ahus1
0
370
Ruby Pattern Matching
bkuhlmann
0
930
サイコロで理解する統計的仮説検定の考え方
tatamiya
4
1k
GitHub Copilotのススメ
marcy731
1
220
Kotlin Multiplatform at Stable and Beyond (Android Makers 2024)
zsmb
0
470
Going beyond Apache Parquet's default settings
xhochy
0
130
Site Reliability Engineering for GMO
pyama86
8
1.1k
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
9
4.2k
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
140
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
423
63k
Gamification - CAS2011
davidbonilla
77
4.6k
Thoughts on Productivity
jonyablonski
60
3.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
352
28k
Statistics for Hackers
jakevdp
790
220k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
126
32k
Designing on Purpose - Digital PM Summit 2013
jponch
111
6.5k
A Modern Web Designer's Workflow
chriscoyier
689
190k
Happy Clients
brianwarren
92
6.4k
Typedesign – Prime Four
hannesfritz
36
2.1k
Writing Fast Ruby
sferik
622
60k
The Illustrated Children's Guide to Kubernetes
chrisshort
32
46k
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!