John Athayde
2005-Jul-27 05:38 UTC
Defining a link_to :action => ''new'' with a predefined variable
I want to create a link_to create on a child of a parent that automatically assigns the id to that field in the DB. Case: Adding an element to a shot. I have it so on the shot page it lists the elements currently in a shot, with a link to "Create New Element", which is called as such: <%=link_to ''Create New Element'', :controller => ''elements'', :action => ''new'' %> How do I define a variable "shot_id" (or element.shot_id) in that link_to? I''ve been paging through the Agile book for a few hours with no luck... Thanks in advance. -- John Athayde bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org Meticulous | www.meticulous.com (work) Rotoscope | www.rotoscope.com (sound: rock band) Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic) Personal Weblog | www.boboroshi.com (play) "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin (1706-1790) Reply of the Pennsylvania Assembly to the Governor November 11, 1755 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Chris Roos
2005-Jul-27 07:54 UTC
Re: Defining a link_to :action => ''new'' with a predefined variable
John Athayde wrote:> I want to create a link_to create on a child of a parent that > automatically assigns the id to that field in the DB. Case: > > Adding an element to a shot. > > I have it so on the shot page it lists the elements currently in a shot, > with a link to "Create New Element", which is called as such: > > <%=link_to ''Create New Element'', :controller => ''elements'', :action => > ''new'' %> > > How do I define a variable "shot_id" (or element.shot_id) in that > link_to? I''ve been paging through the Agile book for a few hours with no > luck... > > Thanks in advance. >Append :id to the end of link_to, as follows. <%= link_to ''Create new element'', :controller => ''elements'', :action => ''new'', :id => @shot.id %> In your elements new action, you can obtain the id from params[:id] This, of course, assumes you have a @shot object accessible to your view. Chris
John Athayde
2005-Jul-27 14:15 UTC
Re: Defining a link_to :action => ''new'' with a predefined variable
That worked! Thanks! This is an extrapolation of the line for the controller: @child["parent_id"] = params[:id] On Jul 27, 2005, at 3:54 AM, Chris Roos wrote:> John Athayde wrote: > >> I want to create a link_to create on a child of a parent that >> automatically assigns the id to that field in the DB. Case: >> Adding an element to a shot. I have it so on the shot page it >> lists the elements currently in a shot, with a link to "Create New >> Element", which is called as such: >> <%=link_to ''Create New Element'', :controller => >> ''elements'', :action => ''new'' %> >> How do I define a variable "shot_id" (or element.shot_id) in that >> link_to? I''ve been paging through the Agile book for a few hours >> with no luck... >> Thanks in advance. >> > Append :id to the end of link_to, as follows. > > <%= link_to ''Create new element'', :controller => > ''elements'', :action => ''new'', :id => @shot.id %> > > In your elements new action, you can obtain the id from params[:id] > > This, of course, assumes you have a @shot object accessible to your > view.-- John Athayde bobo-8WfjrGkLNeNByuSxxbvQtw@public.gmane.org Meticulous | www.meticulous.com (work) Rotoscope | www.rotoscope.com (sound: rock band) Boboroshi & Kynz | www.boboroshiandkynz.com (sound: electronic) Personal Weblog | www.boboroshi.com (play) "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." - Benjamin Franklin (1706-1790) Reply of the Pennsylvania Assembly to the Governor November 11, 1755 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails