hi i am stuck! i have a products controller, model and list view. model states: has_many :versions db table: id, title i have a versions controller, model and list view. model states: belongs_to :products db table: id, num, product_id (hopefully the foreign key!) i want to know how, in this scenario... a user selects a product, say microsoft excel -> versions list for excel are displayed. IVE GOT THIS WORKING! the passed url is: http://localhost:3000/product/show/1 but, supposing i want to add a new version for excel, say program id => ''1'', how do i do this? on the view, i have a button "add new version" this in turn calls a create method in the controller, but i am unsure how i can pass the product_id across to populate the foreign key in the versions table. how can i get the product number, when it is only know from the URL, when passed to enter the associated versions page? from the view: <h1>Add new version</h1> <%= start_form_tag :action => ''create'' %> <p><label for="version_number">Number</label>: <%= text_field ''version'', ''number'' %></p> <%= submit_tag "Create" %> <%= end_form_tag %> from the version controller: # create a new version def create @version = Version.new(params[:version]) if @version.save redirect_to :controller => ''product'', :action => ''show'', :id => @version.product.id end end can you help? do u need more info? HELP!!!!!!!!! i cant find any similar tutorials on this kind of association! i bought the agile dev book, no real use! can you recommened any good sites i could try? or books? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
In your product ''show'' view, for that link to add a new version, it can look something like this: <%= link_to new_version_path(:product => @product.id) %> Clicking that link routes the flow to the versions controller new method. In that method, you''d peek at the params to see if the product was specified, and set the version field if it was: def new @version = Version.new @version.product_id = params[:product] if params[:product] respond_to do |format| format.html # new.html.erb format.xml { render :xml => @scenario } end end which then routes you the the ''new'' view for version. product_id is already set. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Julian Leviston
2008-Apr-03 23:02 UTC
Re: newbie struggling with associations and url passing
Hi! This is assuming you''ve got @product set to the product in question in your CALLING view, and that your version controller is called VersionController (version_controller.rb): <h1>Add new version</h1> <%= start_form_tag :controller => ''version'', :action => ''create'', :product_id => @product.id %> <p><label for="version_number">Number</label>: <%= text_field ''version'', ''number'' %></p> <%= submit_tag "Create" %> <%= end_form_tag %> # create a new version def create @version = Version.new(params[:version]) @version.product_id = params[:product_id] if @version.save redirect_to :controller => ''product'', :action => ''show'', :id => @version.product.id end end The lines to look out for (ie the different ones) are: <%= start_form_tag :controller => ''version'', :action => ''create'', :product_id => @product.id %> and @version.product_id = params[:product_id] Julian. Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3 OUT APRIL 6 http://sensei.zenunit.com/ On 04/04/2008, at 5:09 AM, Brad Symons wrote:> > hi > > i am stuck! > > i have a products controller, model and list view. > model states: has_many :versions > db table: id, title > > i have a versions controller, model and list view. > model states: belongs_to :products > db table: id, num, product_id (hopefully the foreign key!) > > i want to know how, in this scenario... > > a user selects a product, say microsoft excel -> versions list for > excel > are displayed. IVE GOT THIS WORKING! > > the passed url is: http://localhost:3000/product/show/1 > > but, > > supposing i want to add a new version for excel, say program id => > ''1'', > how do i do this? > > on the view, i have a button "add new version" > > this in turn calls a create method in the controller, but i am unsure > how i can pass the product_id across to populate the foreign key in > the > versions table. how can i get the product number, when it is only know > from the URL, when passed to enter the associated versions page? > > from the view: > > <h1>Add new version</h1> > <%= start_form_tag :action => ''create'' %> > <p><label for="version_number">Number</label>: > <%= text_field ''version'', ''number'' %></p> > <%= submit_tag "Create" %> > <%= end_form_tag %> > > from the version controller: > > # create a new version > def create > @version = Version.new(params[:version]) > if @version.save > redirect_to :controller => ''product'', :action => ''show'', :id => > @version.product.id > end > end > > can you help? do u need more info? > > HELP!!!!!!!!! > > i cant find any similar tutorials on this kind of association! i > bought > the agile dev book, no real use! > > can you recommened any good sites i could try? or books? > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---