Luis Bivar Ramos
2013-Oct-14 09:28 UTC
saving to two different models - accepts_nested_attributes_for not working
I''m having trouble with saving to two different models, from one
form... I
tried to follow rails casts #196 and looked at several other examples here
at stackoverflow but I haven''t figured out how to do this because my
case
is a little bit different...
So, I have two models:
class Patient < ActiveRecord::Base
attr_accessible :date_of_birth, :patient_name
has_many :samples
end
class Sample < ActiveRecord::Base
attr_accessible :approved, :patientID, :result, :patient
belongs_to :patient
accepts_nested_attributes_for :patient
end
What I found on most of the examples I saw, including the railscasts, is to
create a new sample inside the patient form, and the
"accepts_nestes_attributes_for" is inside the "patient"
model... But what I
want to do is exactly the opposite, that is, creating a new patient inside
the new sample form.
Here''re my views, I have a normal form for the sample and then a
partial
for the patient part:
_form.html.erb
<%= form_for(@sample) do |f| %>
<% if @sample.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@sample.errors.count, "error") %>
prohibited this sample from being saved:</h2>
<ul>
<% @sample.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :result %><br />
<%= f.text_area :result %>
</div>
<div class="field">
<%= f.label :approved %><br />
<%= f.check_box :approved %>
</div>
<div><%= render ''patient'' %></div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
patient.html.erb
<%= fields_for :patient do |p| %>
<div class="field">
<%= p.label :patient_name %><br />
<%= p.text_field :patient_name %>
</div>
<div class="field">
<%= p.label :date_of_birth %><br />
<%= p.date_select :date_of_birth %>
</div>
<% end %>
When I click the submit button everything seems to work, but nothing is
saved into the patients table.
Here''s the log:
Started POST "/samples" for 127.0.0.1 at 2013-10-12 23:32:03 +0100
Processing by SamplesController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"13OKZ8DaGJ4zTb35q+ymSzx7r+Ipxou1u+XrR4jtyeI=",
"sample"=>{"result"=>"teste 3",
"approved"=>"0"},
"patient"=>{"patient_name"=>"Joao",
"date_of_birth(1i)"=>"2013",
"date_of_birth(2i)"=>"10",
"date_of_birth(3i)"=>"10"},
"commit"=>"Create Sample"}
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "samples" ("approved",
"created_at", "patientID", "result",
"updated_at") VALUES (?, ?, ?, ?, ?) [["approved", false],
["created_at", Sat, 12 Oct 2013 22:32:03 UTC +00:00],
["patientID", nil], ["result", "teste 3"],
["updated_at", Sat, 12 Oct 2013 22:32:03 UTC +00:00]]
(2.4ms) commit transaction
Redirected to http://localhost:3000/samples/3
Completed 302 Found in 6ms (ActiveRecord: 2.9ms)
As you can see in the log, it''s only saving to the sample table. Can I
use
accepts_nested_attributes_for this way? Is there a way to achieve what I
want? Or should I try a different approach?
After I solve this I will try to check first if the patient exists or not
before committing and try to do as SAYT on the patient''s name... but
that''s
a future step!
Thanks for your help
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/a6de505b-1fa1-4994-b9c0-d296e3b8ad63%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Luis Bivar Ramos
2013-Oct-15 23:35 UTC
Re: saving to two different models - accepts_nested_attributes_for not working
I''ve also have this question posted on stackoverflow, and there''s a little bit more info there because of a reply I''ve got. If you want to take a look into it: http://stackoverflow.com/questions/19340165/saving-to-two-different-models-accepts-nested-attributes-for-not-working/19369601?noredirect=1#comment28708707_19369601 Any more ideas on how I might solve this? Segunda-feira, 14 de Outubro de 2013 10:28:12 UTC+1, Luis Bivar Ramos escreveu:> > I''m having trouble with saving to two different models, from one form... I > tried to follow rails casts #196 and looked at several other examples here > at stackoverflow but I haven''t figured out how to do this because my case > is a little bit different... > > So, I have two models: > > class Patient < ActiveRecord::Base > attr_accessible :date_of_birth, :patient_name > > has_many :samples > end > > class Sample < ActiveRecord::Base > attr_accessible :approved, :patientID, :result, :patient > > belongs_to :patient > accepts_nested_attributes_for :patient > end > > What I found on most of the examples I saw, including the railscasts, is > to create a new sample inside the patient form, and the > "accepts_nestes_attributes_for" is inside the "patient" model... But what I > want to do is exactly the opposite, that is, creating a new patient inside > the new sample form. > > Here''re my views, I have a normal form for the sample and then a partial > for the patient part: > > _form.html.erb > > <%= form_for(@sample) do |f| %> > <% if @sample.errors.any? %> > <div id="error_explanation"> > <h2><%= pluralize(@sample.errors.count, "error") %> prohibited this sample from being saved:</h2> > > <ul> > <% @sample.errors.full_messages.each do |msg| %> > <li><%= msg %></li> > <% end %> > </ul> > </div> > <% end %> > > <div class="field"> > <%= f.label :result %><br /> > <%= f.text_area :result %> > </div> > <div class="field"> > <%= f.label :approved %><br /> > <%= f.check_box :approved %> > </div> > <div><%= render ''patient'' %></div> > <div class="actions"> > <%= f.submit %> > </div> > <% end %> > > patient.html.erb > > <%= fields_for :patient do |p| %> > <div class="field"> > <%= p.label :patient_name %><br /> > <%= p.text_field :patient_name %> > </div> > <div class="field"> > <%= p.label :date_of_birth %><br /> > <%= p.date_select :date_of_birth %> > </div> > <% end %> > > When I click the submit button everything seems to work, but nothing is > saved into the patients table. > > Here''s the log: > > Started POST "/samples" for 127.0.0.1 at 2013-10-12 23:32:03 +0100 > Processing by SamplesController#create as HTML > Parameters: {"utf8"=>"✓", "authenticity_token"=>"13OKZ8DaGJ4zTb35q+ymSzx7r+Ipxou1u+XrR4jtyeI=", "sample"=>{"result"=>"teste 3", "approved"=>"0"}, "patient"=>{"patient_name"=>"Joao", "date_of_birth(1i)"=>"2013", "date_of_birth(2i)"=>"10", "date_of_birth(3i)"=>"10"}, "commit"=>"Create Sample"} > (0.1ms) begin transaction > SQL (0.5ms) INSERT INTO "samples" ("approved", "created_at", "patientID", "result", "updated_at") VALUES (?, ?, ?, ?, ?) [["approved", false], ["created_at", Sat, 12 Oct 2013 22:32:03 UTC +00:00], ["patientID", nil], ["result", "teste 3"], ["updated_at", Sat, 12 Oct 2013 22:32:03 UTC +00:00]] > (2.4ms) commit transaction > Redirected to http://localhost:3000/samples/3 > Completed 302 Found in 6ms (ActiveRecord: 2.9ms) > > As you can see in the log, it''s only saving to the sample table. Can I use > accepts_nested_attributes_for this way? Is there a way to achieve what I > want? Or should I try a different approach? > > After I solve this I will try to check first if the patient exists or not > before committing and try to do as SAYT on the patient''s name... but that''s > a future step! > > Thanks for your help >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/73c45015-b682-494c-879f-b0f06a812ea5%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.