views:

44

answers:

2

I have this link to create a new page:

"new" %>

The code called in the controller is this:

def new @page = Page.new @visuals = Visual.find(:all) end

I would like to pass an additional parameter to pre-set a field in the form, something like this:

"new/parent_id/2" %>

and in the controller

def new @page = Page.new @page.parent_id = params[:parent_id] @visuals = Visual.find(:all) end

+1  A: 

To get a new parameter without changing routes, just pass it in as a query string:

new?parent_id=2

then it will be available to you as params[:parent_id]

Michael Sepcot
+1  A: 
link_to "new", :action => 'new', :parent_id => 1

... will give you:

/controller/new?parent_id=1

If you mess with the routes, you could do:

map.new_page_from_parent "/new_from_parent/:parent_id"

... and use like so:

link_to "new", new_page_from_parent_path(:parent_id => 1)

# gives
/new_from_parent/1

# accessed via
params[:parent_id] => 1
wesgarrison
wes thanks, it's a really clean and complete answer!
rtacconi