What would be considered the "standard" way of setting the foreign key on a child record when starting from a view of the parent record? E.g. you''re on a view to a person record and use a link_to load a form that will enable you to add an address for that person (addresses are stored in a separate table): <%= link_to ''New address'', { :controller => ''addresses'', :action => ''new'' } %> The create action in the addresses controller needs to set the person_id. Thanks for your input. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
William Pratt
2007-Sep-16 15:31 UTC
Re: Passing foreign key when creating new child record
One way is to add person_id to the link to the address construction form: <%= link_to ''New address'', { :controller => ''addresses'', :action =>''new'', :person_id => person } %> Then pass the person_id to the address construction view and store it in a hidden field then in the create method: @person = Person.find(params[:person_id]) @person.create_address(params[:address]) Chris Bartlett wrote:> What would be considered the "standard" way of setting the foreign key > on a child record when starting from a view of the parent record? E.g. > you''re on a view to a person record and use a link_to load a form that > will enable you to add an address for that person (addresses are > stored in a separate table): > > <%= link_to ''New address'', { :controller => ''addresses'', :action => > ''new'' } %> > > The create action in the addresses controller needs to set the > person_id. > > Thanks for your input. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Bartlett
2007-Sep-17 07:54 UTC
Re: Passing foreign key when creating new child record
Thanks for the pointer. I''d looked around and couldn''t find a clear example. Here''s the relevant code in the various files I used to get this working. views/people/show.rhtml <%= link_to ''New address'', { :controller => ''addresses'', :action => ''new'', :person_id => @person } %> views/addresses/new.rhtml <% form_for :address, :url => { :action => :create } do |form| %> <%= form.hidden_field :person_id, :value => params[:person_id] %> [... other form fields, save button, etc.] <% end %> models/person.rb class Person < ActiveRecord::Base has_many :addresses end models/address.rb class Address < ActiveRecord::Base belongs_to :person # So the addresses table needs a ''person_id'' column, which is the foreign key end controllers/addresses_controller.rb def new @address = Address.new end def create @address = Address.new(params[:address]) [...] end On Sep 17, 3:31 am, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> One way is to add person_id to the link to the address construction form: > > <%= link_to ''New address'', { :controller => ''addresses'', :action > =>''new'', :person_id => person } %> > > Then pass the person_id to the address construction view and store it in > a hidden field > > then in the create method: > @person = Person.find(params[:person_id]) > @person.create_address(params[:address]) > > Chris Bartlett wrote: > > What would be considered the "standard" way of setting the foreign key > > on a child record when starting from a view of the parent record? E.g. > > you''re on a view to a person record and use a link_to load a form that > > will enable you to add an address for that person (addresses are > > stored in a separate table): > > > <%= link_to ''New address'', { :controller => ''addresses'', :action => > > ''new'' } %> > > > The create action in the addresses controller needs to set the > > person_id. > > > Thanks for your input.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---