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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Hassan Schroeder at Jan 20, 2012 at 12:55 am

    On Thu, Jan 19, 2012 at 4:19 PM, PierreW 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 Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • PierreW at Jan 20, 2012 at 8:19 am
    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:55 am, Hassan Schroeder wrote:
    On Thu, Jan 19, 2012 at 4:19 PM, PierreW 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 Schroeder ------------------------ hassan.schroe...@gmail.comhttp://about.me/hassanschroeder
    twitter: @hassan
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Hassan Schroeder at Jan 20, 2012 at 4:00 pm

    On Fri, Jan 20, 2012 at 12:18 AM, PierreW 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 :-)

    --
    Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • PierreW at Jan 20, 2012 at 6:36 pm
    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:00 pm, Hassan Schroeder wrote:
    On Fri, Jan 20, 2012 at 12:18 AM, PierreW 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 :-)

    --
    Hassan Schroeder ------------------------ hassan.schroe...@gmail.comhttp://about.me/hassanschroeder
    twitter: @hassan
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Hassan Schroeder at Jan 20, 2012 at 6:55 pm

    On Fri, Jan 20, 2012 at 10:35 AM, PierreW 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 Schroeder ------------------------ hassan.schroeder@gmail.com
    http://about.me/hassanschroeder
    twitter: @hassan

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • PierreW at Jan 20, 2012 at 7:08 pm
    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:55 pm, Hassan Schroeder wrote:
    On Fri, Jan 20, 2012 at 10:35 AM, PierreW 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 Schroeder ------------------------ hassan.schroe...@gmail.comhttp://about.me/hassanschroeder
    twitter: @hassan
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 20, '12 at 12:19a
activeJan 20, '12 at 7:08p
posts7
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

PierreW: 4 posts Hassan Schroeder: 3 posts

People

Translate

site design / logo © 2022 Grokbase