Prevent escaped ampersand in query string

1k Views Asked by At

In my mailer, I am generating a link to my sign up form so that I can include it in the email to the user. I'd like to prepopulate the sign up form with the user's name and email address.

I create an instance variable @signup_query like so:

@signup_query = "?name=" << 
                CGI.escape(@name) << 
                "&" << 
                "email=" << 
                CGI.escape(@email)

But when I call link_to in my view, Rails encodes the query string, which adds 'amp;' to the '&'. As a result, my params hash has key 'amp;email' instead of key 'email' so in my sign up view, I have this ugliness:

= f.email_field :email, class: 'form-control', :value => params['amp;email']

This works but I know there must be a better way.

1

There are 1 best solutions below

3
On

Try this

link_to "Sign up", my_signup_url(name: CGI.escape(@name), email: CGI.escape(@email))

in your mailer.

For more details on link_to use this