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
370
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
79
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
65
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
56
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
80
GDG Meetup - Carreiras em T.I.
jackson_pires
0
82
20 minutos insanos de TDD e Ruby
jackson_pires
0
110
Ruby on Rails para Iniciantes - Aula 46
jackson_pires
0
160
Ruby on Rails para Iniciantes - Aula 47
jackson_pires
0
80
Ruby on Rails para Iniciantes - Aula 49
jackson_pires
0
150
Other Decks in Programming
See All in Programming
MCPで実現するAIエージェント駆動のNext.jsアプリデバッグ手法
nyatinte
7
1k
ProxyによるWindow間RPC機構の構築
syumai
2
660
【第4回】関東Kaggler会「Kaggleは執筆に役立つ」
mipypf
0
1k
複雑なドメインに挑む.pdf
yukisakai1225
5
920
「手軽で便利」に潜む罠。 Popover API を WCAG 2.2の視点で安全に使うには
taitotnk
0
290
Design Foundational Data Engineering Observability
sucitw
2
130
ECS初心者の仲間 – TUIツール「e1s」の紹介
keidarcy
0
150
Namespace and Its Future
tagomoris
6
690
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
360
TDD 実践ミニトーク
contour_gara
1
280
250830 IaCの選定~AWS SAMのLambdaをECSに乗り換えたときの備忘録~
east_takumi
0
370
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
1.1k
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
358
30k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.6k
Rails Girls Zürich Keynote
gr2m
95
14k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Building Applications with DynamoDB
mza
96
6.6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
Why Our Code Smells
bkeepers
PRO
339
57k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
800
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
61k
Producing Creativity
orderedlist
PRO
347
40k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
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!