Merging URL parameters with form_for ones

Hello!

I am using the standard (scaffolded), REST routing. I need to pass
external values to my “new” action for a certain model, and let the
user complete the other fields in the standard form_for.
My initial idea has been to simply amend the URL like this:

…/foos/new?size=4&weight=2

Then in the controller I just do something like:

def new
@temp = Foo.new
@temp.size = params[:size]

respond_to do |format|
format.html
format.xml { render :xml => @temp }
end

end

Thing is my form_for parameters are not picked up. Only the ones I
have setup manually in the URL (size and weight in my example) are
taken into account.
I believe it is due to the fact that they are serialized and added to
the end of the URL, whereas it already contains a “?” and some
parameters so they end up being ignored.

Do you know how to do this?

Thanks!
PJ

On Thu, Jan 19, 2012 at 4:19 PM, PierreW [email protected]
wrote:

I am using the standard (scaffolded), REST routing. I need to pass
external values to my “new” action for a certain model, and let the
user complete the other fields in the standard form_for.
My initial idea has been to simply amend the URL like this:

…/foos/new?size=4&weight=2

Why don’t you include them in the form as hidden fields?


Hassan S. ------------------------ [email protected]

twitter: @hassan

Thanks Hassan.

Sorry I did not give the context: these links are used by another,
external app (say app B). A user of app B can trigger the creation of
a record in App A, and it needs to pass some default parameters. So I
don’t believe I can use the standard method of using hidden fields,
can I?

So the idea really is: how can you create entry points to an app where
these entry points contain default parameters for the creation of a
record? It seems to me these parameters HAVE to be passed with the
URL, but maybe I am wrong?

Thanks!

On Jan 20, 12:55am, Hassan S. [email protected]

On Fri, Jan 20, 2012 at 12:18 AM, PierreW [email protected]
wrote:

Sorry I did not give the context: these links are used by another,
external app (say app B). A user of app B can trigger the creation of
a record in App A, and it needs to pass some default parameters.

OK, so to make sure I understand:

app B has a link to appA/foos/new?size=4&weight=2 – correct?

and then appA’s foos create action should include those additional
parameters passed to new?

If so, there’s no reason you couldn’t conditionally add hidden inputs
to your new form to be processed as part of the create.

Please let me know if I’m totally off base on my understanding :slight_smile:


Hassan S. ------------------------ [email protected]

twitter: @hassan

Your understanding is absolutely right.

My initial gut was indeed to try hidden inputs. But it does not work:

App B calls appA/foos/new?size=4&weight=2

App A foo/new method is called and the form is pre-populated with size
and weight.
The problem is, the URL is still appA/foos/new?size=4&weight=2. When
the form is submitted, I think what happens is Rails tries to append
the form parameters to the existing URL. Since that URL already
contains parameters it does something like:

appA/foos/new?size=4&weight=2?size=4&weight=2&price=10 etc and the
second set of parameters after the second “?” is ignored by the
browser.

At least that’s my understanding (I could be wrong).

I guess it would work if I could remove the first set of params. But
it looks like I will need to do a redirect_to. I was thinking there
might be a cleaner way.

Sorry it is a bit convoluted…

Thanks!
PJ

On Jan 20, 4:00pm, Hassan S. [email protected]

On Fri, Jan 20, 2012 at 10:35 AM, PierreW [email protected]
wrote:

App A foo/new method is called and the form is pre-populated with size
and weight.
The problem is, the URL is still appA/foos/new?size=4&weight=2. When
the form is submitted, I think what happens is Rails tries to append
the form parameters to the existing URL.

Sorry, that makes no sense – the submission URL (that will go to the
create action) shouldn’t contain anything from the new request.

What is your view code for the start of the form, and what does using
view source show you for that?


Hassan S. ------------------------ [email protected]

twitter: @hassan

Hassan,

Sorry, that makes no sense – the submission URL (that will go to the
create action) shouldn’t contain anything from the new request.

You are absolutely right… I got totally confused. I looked at my
form again and indeed the problem was in it.

Thanks a lot for pointing me in the right direction.

PJ

On Jan 20, 6:55pm, Hassan S. [email protected]