I''m writing a simple phonelist to help me get familiar with Rails. I create a Contact object and when it''s Create method completes it redirects to another objects New method passing the Contact ID to the contact_id field of the related object. This works fine until I try and Create the related object and because the contact_id is in the URL (ie http://..../new?contact_id=2) and the rest of the fields are in the form it doesn''t add the contact_id to the new object. How should I go about passing this contact_id to the related object?? Scott Arthur
redirect_to :controller=>''other_object'', :action=>''new'', :contact_id=>''whatever your id is'' I''m quite sure I did not understand exactly what you meant, so there is a chance this wont help you. On 9/8/05, Scott Arthur <scott-2vIkDnebgV7SUeElwK9/Pw@public.gmane.org> wrote:> > I''m writing a simple phonelist to help me get familiar with Rails. > > I create a Contact object and when it''s Create method completes it > redirects to another objects New method passing the Contact ID to the > contact_id field of the related object. > > This works fine until I try and Create the related object and because > the contact_id is in the URL (ie http://..../new?contact_id=2) and > the rest of the fields are in the form it doesn''t add the contact_id > to the new object. > > How should I go about passing this contact_id to the related object?? > > Scott Arthur > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 8.9.2005, at 8.33, Scott Arthur wrote:> I''m writing a simple phonelist to help me get familiar with Rails. > > I create a Contact object and when it''s Create method completes it > redirects to another objects New method passing the Contact ID to > the contact_id field of the related object. > > This works fine until I try and Create the related object and > because the contact_id is in the URL (ie http://..../new? > contact_id=2) and the rest of the fields are in the form it doesn''t > add the contact_id to the new object. > > How should I go about passing this contact_id to the related object??Two ways: 1. <%= form_tag :controller => ''foo'', :action => ''new_object'', :id => params[:contact_id] %> 2. Let the form_tag be and instead put a hidden field in the form: <% = hidden_field_tag "contact_id", params[:contact_id] %> This way the id will be passed along to the action that handles the form submission. Note that in the first case the id will be params [:id], not params[:contact_id]. //jarkko> > Scott Arthur > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Jarkko Have tried both these to the best of my understanding, with no luck. I probably needed to spend time explaining my issue. I have a contacts controller and an ice_contacts controller. The contacts ''Create'' method looks like this def create @contact = Contact.new(params[:contact]) if @contact.save flash[:notice] = ''Contact was successfully created.'' redirect_to :controller => ''ice_contacts'', :action => ''new'', :contact_id => @contact.id else render :action => ''new'' end end The problem that I see with this is that the contact_id field ends up in the url of the new ice_contact object (ie ice_contact/new? contact_id=6) which is a GET variable and the rest of the ice_contact fields are in the form, which are POST variables. I hope I''m making sense. Any help much appreciated. On 8/09/2005, at 7:03 PM, Jarkko Laine wrote:> > On 8.9.2005, at 8.33, Scott Arthur wrote: > >> I''m writing a simple phonelist to help me get familiar with Rails. >> >> I create a Contact object and when it''s Create method completes it >> redirects to another objects New method passing the Contact ID to >> the contact_id field of the related object. >> >> This works fine until I try and Create the related object and >> because the contact_id is in the URL (ie http://..../new? >> contact_id=2) and the rest of the fields are in the form it >> doesn''t add the contact_id to the new object. >> >> How should I go about passing this contact_id to the related object?? > > Two ways: > 1. <%= form_tag :controller => ''foo'', :action => ''new_object'', :id > => params[:contact_id] %> > 2. Let the form_tag be and instead put a hidden field in the form: < > %= hidden_field_tag "contact_id", params[:contact_id] %> > > This way the id will be passed along to the action that handles the > form submission. Note that in the first case the id will be params > [:id], not params[:contact_id]. > > //jarkko > >> >> Scott Arthur >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Friday 09 September 2005 01:42, Scott Arthur wrote:> Hi Jarkko > > Have tried both these to the best of my understanding, with no luck. > > I probably needed to spend time explaining my issue. > > I have a contacts controller and an ice_contacts controller. > > The contacts ''Create'' method looks like this > > def create > @contact = Contact.new(params[:contact]) > if @contact.save > flash[:notice] = ''Contact was successfully created.'' > redirect_to :controller => ''ice_contacts'', :action => > ''new'', :contact_id => @contact.id > else > render :action => ''new'' > end > end > > The problem that I see with this is that the contact_id field ends up > in the url of the new ice_contact object (ie ice_contact/new? > contact_id=6) which is a GET variable and the rest of the ice_contact > fields are in the form, which are POST variables.To Rails it makes no difference where the parameters go. Try it like this redirect_to :controller => ''ice_contacts'', :action => ''new'', :ice_contact => {:contact_id => @contact.id} Then, in your IceContactController, you need to pick up this parameter def create @ice_contact = IceContact.new(params[:ice_contact]) end I haven''t tried it exactly like this, but have experimented with a similar setup, to pass parameters from one form to the next. Michael -- Michael Schuerig Airtight arguments have mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions
On 9.9.2005, at 2.42, Scott Arthur wrote:> Hi Jarkko > > Have tried both these to the best of my understanding, with no luck. > > I probably needed to spend time explaining my issue. > > I have a contacts controller and an ice_contacts controller. > > The contacts ''Create'' method looks like this > > def create > @contact = Contact.new(params[:contact]) > if @contact.save > flash[:notice] = ''Contact was successfully created.'' > redirect_to :controller => ''ice_contacts'', :action => > ''new'', :contact_id => @contact.id > else > render :action => ''new'' > end > end > > The problem that I see with this is that the contact_id field ends > up in the url of the new ice_contact object (ie ice_contact/new? > contact_id=6)That is why I proposed naming it :id, not :contact_id. Default Rails routing will use id as the last part of the url, so your url would become ice_contact/new/6. That way it is part of the "real" url, not a query variable. You could then change the new action to handle the id accordingly.> which is a GET variable and the rest of the ice_contact fields are > in the form, which are POST variables.You can solve this by the method above or using a hidden_field in create.rhtml. That way the contact id will be passed just like all other POST variables. Alas, I described both methods in my previous post. Just pick the method that suits you best. If you don''t want to change the action new, use the hidden field approach.> > I hope I''m making sense. Any help much appreciated.I think you are making sense unless I have totally misunderstood you. Actually you made sense already the first time, you just didn''t quite obey my instructions ;-) //jarkko> > > On 8/09/2005, at 7:03 PM, Jarkko Laine wrote: > >> >> On 8.9.2005, at 8.33, Scott Arthur wrote: >> >>> I''m writing a simple phonelist to help me get familiar with Rails. >>> >>> I create a Contact object and when it''s Create method completes >>> it redirects to another objects New method passing the Contact ID >>> to the contact_id field of the related object. >>> >>> This works fine until I try and Create the related object and >>> because the contact_id is in the URL (ie http://..../new? >>> contact_id=2) and the rest of the fields are in the form it >>> doesn''t add the contact_id to the new object. >>> >>> How should I go about passing this contact_id to the related >>> object?? >> >> Two ways: >> 1. <%= form_tag :controller => ''foo'', :action => ''new_object'', :id >> => params[:contact_id] %> >> 2. Let the form_tag be and instead put a hidden field in the form: >> <%= hidden_field_tag "contact_id", params[:contact_id] %> >> >> This way the id will be passed along to the action that handles >> the form submission. Note that in the first case the id will be >> params[:id], not params[:contact_id]. >> >> //jarkko-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails