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.
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?
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.