Hi, I''ve done some searching but haven''t yet found if there is a "correct" way to do this. I have an existing URL (generated, for example, by a call to one of my named-route''s helpers: login_url). I may (or may not) have, as part of this URL, a query string fragment ("?test=true" for example) as part of the URL. I also have 1 (or more) name/value pairs I''d like to add, as query parameters, to this URL, overwriting existing values if there is a name collision. I could write up a method to do this pretty easily. If, however, there is already rails code and/or a "recommended," "official," or "canonical" way to do this, I''d like to know.
Philip Hallstrom
2009-May-13 20:23 UTC
Re: Canonical Way/Method to Add/Merge Query Parameters onto URL
Let''s say that your controller gets run with params[:test] => ''true''... Then in your controller or view you can do: login_url(params.merge(:test => false, :foo => ''bar'')) and will end up with ..../login?test=false&foo=bar Look at the merge and reverse_merge methods for more. -philip On May 13, 2009, at 1:11 PM, Kendall wrote:> > Hi, I''ve done some searching but haven''t yet found if there is a > "correct" way to do this. > > I have an existing URL (generated, for example, by a call to one of my > named-route''s helpers: login_url). I may (or may not) have, as part of > this URL, a query string fragment ("?test=true" for example) as part > of the URL. I also have 1 (or more) name/value pairs I''d like to add, > as query parameters, to this URL, overwriting existing values if there > is a name collision. > > I could write up a method to do this pretty easily. If, however, there > is already rails code and/or a "recommended," "official," or > "canonical" way to do this, I''d like to know. > >
Kendall
2009-May-13 20:58 UTC
Re: Canonical Way/Method to Add/Merge Query Parameters onto URL
Thanks. Very helpful! On May 13, 2:23 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote:> Let''s say that your controller gets run with params[:test] => ''true''... > > Then in your controller or view you can do: > > login_url(params.merge(:test => false, :foo => ''bar'')) > > and will end up with > > ..../login?test=false&foo=bar > > Look at the merge and reverse_merge methods for more. > > -philip > > On May 13, 2009, at 1:11 PM,Kendallwrote: > > > > > Hi, I''ve done some searching but haven''t yet found if there is a > > "correct" way to do this. > > > I have an existing URL (generated, for example, by a call to one of my > > named-route''s helpers: login_url). I may (or may not) have, as part of > > this URL, a query string fragment ("?test=true" for example) as part > > of the URL. I also have 1 (or more) name/value pairs I''d like to add, > > as query parameters, to this URL, overwriting existing values if there > > is a name collision. > > > I could write up a method to do this pretty easily. If, however, there > > is already rails code and/or a "recommended," "official," or > > "canonical" way to do this, I''d like to know.