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
80
Conhecendo o módulo Forwardable do Ruby
jackson_pires
0
66
COMO INSPIRAR PESSOAS E MONETIZAR O SEU CONHECIMENTO?
jackson_pires
0
57
Conhecendo a gem guard, guard-rspec e guard-livereload
jackson_pires
0
80
GDG Meetup - Carreiras em T.I.
jackson_pires
0
84
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
80
Ruby on Rails para Iniciantes - Aula 49
jackson_pires
0
160
Other Decks in Programming
See All in Programming
スキーマ駆動で、Zod OpenAPI Honoによる、API開発するために、Hono Takibiというライブラリを作っている
nakita628
0
310
Claude Agent SDK を使ってみよう
hyshu
0
1.4k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
440
マンガアプリViewerの大画面対応を考える
kk__777
0
140
なぜGoのジェネリクスはこの形なのか? - Featherweight Goが明かす設計の核心
qualiarts
0
240
O Que É e Como Funciona o PHP-FPM?
marcelgsantos
0
190
Developer Joy - The New Paradigm
hollycummins
1
360
Devoxx BE - Local Development in the AI Era
kdubois
0
140
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
530
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
600
One Enishi After Another
snoozer05
PRO
0
150
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
1.2k
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Faster Mobile Websites
deanohume
310
31k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
How to train your dragon (web standard)
notwaldorf
97
6.3k
Keith and Marios Guide to Fast Websites
keithpitt
411
23k
Side Projects
sachag
455
43k
Code Reviewing Like a Champion
maltzj
526
40k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.5k
Designing Experiences People Love
moore
142
24k
Agile that works and the tools we love
rasmusluckow
331
21k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
The Straight Up "How To Draw Better" Workshop
denniskardys
238
140k
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!