Hey there,
I have a patients model which has many hospitalizations, from the
patients#show I want to generate a link to hospitalizations#new creating
a
new association to this very same patient, any ideas on how to do that?
I
want it to when I get to the hospitalizations#ne I be able to manipulate
the output the patient data in the generated view and submit a
Patient.hospitalizations.create, how would be the best approach for
that?
Thanks in advance,
Diego Dillenburg Bueno
Graduando em Ciências da Computação
UNESP - Rio Claro
(12) 98116-7741
https://www.facebook.com/diegodillenburg
http://br.linkedin.com/in/diegodillenburg
https://github.com/diegodillenburg
Further updates on my question above:
what if I have a helper method to store the “current_patient”, having
always the last patient from my search query and on my
hospitalizations_controller I build the Create method something like
this:
@hospitalization =
current_patient.hospitalizations.new(hospitalization_params) ? Would
this
be the best way to work around this?
Diego Dillenburg Bueno
Graduando em Ciências da Computação
UNESP - Rio Claro
(12) 98116-7741
https://www.facebook.com/diegodillenburg
http://br.linkedin.com/in/diegodillenburg
https://github.com/diegodillenburg
2015-01-13 10:50 GMT-02:00 Diego Dillenburg Bueno
<[email protected]
:
On Tuesday, 13 January 2015 11:22:14 UTC-5, Diego Dillenburg Bueno
wrote:
This is a great use case for nested resource routing - you aren’t just
trying to make a hospitalization, you’re trying to make one for a
particular patient. So instead of a route like:
/hospitalizations/new
you’d want to use:
/patients/:patient_id/hospitalizations/new
One way to read this is “make a new hospitalization for the patient
:patient_id”.
To do this in the routes file, you’d have something like:
resources :patients do
resources :hospitalizations
end
You can make a link to the new page with something like (in an ERB
template):
link_to “New Hospitalization”,
new_patient_hospitalization_path(@patient)
assuming you’ve got the desired patient record in @patient.
–Matt J.
Also checkout accepted_nested_resource I think it is? I’m on my phone
currently…this would be placed in your model
–Rob
Sent from my cell, please excuse any typos.
On Jan 15, 2015 1:04 PM, “Diego Dillenburg Bueno”
[email protected]
Thanks for the reply. That would mean building a namespace like Patients
Hospitalization? Nesting Hospitalization inside Patients, right? Will
try
this. For now I had solved this issue storing the patient_id in the
session, any information on this solution? If it’s ok or not to use it
like
that?
Diego Dillenburg Bueno
Graduando em Ciências da Computação
UNESP - Rio Claro
(12) 98116-7741
https://www.facebook.com/diegodillenburg
http://br.linkedin.com/in/diegodillenburg
https://github.com/diegodillenburg
2015-01-15 2:17 GMT-02:00 Matt J. [email protected]:
On Jan 15, 2015, at 11:03 AM, Diego Dillenburg Bueno
[email protected] wrote:
For now I had solved this issue storing the patient_id in the session, any
information on this solution?
That sounds like a bad idea. What if the user has more than one window
open, or manages to use the back button to get to a hospitalization for
a different patient?
I’d think that patient_id for a new hospitalization ought to go into a
hidden field in the form.
–
Scott R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice
Nested resource is the way to go.
–Rob
Sent from my cell, please excuse any typos.
Some resources:
I was wrong. It’s accepts_nested_attributes_for
http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for.
Sorry, case of not having Google available to me.
http://asciicasts.com/episodes/196-nested-model-form-part-1
http://asciicasts.com/episodes/197-nested-model-form-part-2
This should help you with your problem. You need to do nested
resources…it’s the right thing to do. PS railscasts is a handy
resource to have at hand. Ryan B. needs to start doing them again!
–rob