views:

332

answers:

2

I'm attempting to create a hierarchal menu system in rails. My menu model is in a tree data structure, and I need to pass the parent information to the new action. The best way i found to do this was to pass the parent's id as a querystring. This works, unless validation blocks creation. The problem is that after the new view is rendered, the parameter is no longer present in the url.

Am i going the wrong way with this? Should I just be persisting the value in the session? Should i create a dropdown to pick the parent item (I'd prefer to just let the user click "Add new item here"). This must be a common issue, but I havent been able to find antything relevant.

Update: To clarify a question, I tried doing the following to pass the parameter when the view is rendered in the failure case, but the url has already been set at that point (we've already posted to the controller's base url).

format.html { render :action => "new", :parent_id=> params[:parent_id] }

I need another way to pass the parameter.

A: 

Not 100% clear on what you are trying to achieve.

After the operation fails validation, you render the view again? Can you not just pass the parent_id to this view as a parameter?

Toby Hede
I edited the question to hopefully clarify
midas06
A: 

I figured it out. I just added a hidden field to hold the parent_id. When the browser requests the new form, i put the parent_id into the model object, and it's persisted on psotback.

midas06