I'm trying to get Rails generated _path methods to give me
/terms_and_conditions?utm_source=source&utm_campaign=pain#some_link
So I've tried the following in a controller:
utm_params = {"utm_source" => "sauce", "utm_campaign" => "pain"} # from params
redirect_to terms_and_conditions_path(utm_params.merge(anchor: 'some_link')
and I get this, with the anchor as a param:
/terms_and_conditions?anchor=some_link&utm_source=source&utm_campaign=pain
If I try
terms_and_conditions_path(utm_params, anchor: 'some_link')
I get the hash in place of format, instead of proper params (i.e. no ?
):
/terms_and_conditions.utm_source=source&utm_campaign=pain#some_link
How am I supposed to pass params on AND add an anchor?
Well this is bonkers.
Looks like the problem is Hash vs HashWithIndifferentAccess; when you do
params.to_h
, you get aHashWithIndifferentAccess
- which the url helpers don't treat like aHash
, and can't pull the anchor out of - so the anchor becomes part of the params.params.to_hash
gives you a "real" hash; merginganchor: 'blah'
works as expected.(note: originally, I thought it was because the keys were strings - but it was a red herring;
HashWithIndifferentAccess#symbolize_keys
returns a real hash)At this point, I'd love to reference the autogenerated
x_path
api docs, but I'm pretty sure that just doesn't exist, and the code is to confusing to follow. Trial and error won the day here.