Newbie struggling with associations and url passing

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:

Add new version

<%= start_form_tag :action => 'create' %>

Number: <%= text_field 'version', 'number' %>

<%= 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?

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.

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):

Add new version

<%= start_form_tag :controller => 'version', :action => 'create', :product_id => @product.id %>

Number: <%= text_field 'version', 'number' %>

<%= 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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs