0

I've got a route setup:

get 'e/:email' => "static#home"

So in the controller I want to get the email via params[:email]

Somehow this won't work, as I open the URL it says:

No route matches [GET] "/e/test%40gmail.com"

How would you accomplish this? Cheers!

davegson
  • 8,205
  • 4
  • 51
  • 71

1 Answers1

1

Try this:

get 'e/:email' => "static#home", :format => false, :constraints => { :email => /[^\/]+/ }

Explanation:

By default, rails won't allow a . in a parameter.

I just tried implementing an equivalent route in my own rails app, and it worked. The params hash looked like this:

{"controller"=>"pages", "action"=>"home", "email"=>"test@gmail", "format"=>"com"}

The reason it still worked for me is that by default, rails assumes anything after the . is the format parameter (the format param is normally used, eg, to specify that you want json - eg. example.com/my_controller/index.json).

Since you're getting the error, you must have things set up to ignore that format parameter that is included by default.

So, you should be able to reproduce the result I got (ie, a matching route, but with the .com part in the format param, not the email param) with the following:

get 'e/:email' => "static#home", :format => true

But to solve your problem, and continue to exclude format param, but to include the ., and everything after it, as part of your email param, you need to pass through that last option, :constraints => { :email => /[^\/]+/ } which basically says to allow anything but a slash in the email param.

More info here: Why do routes with a dot in a parameter fail to match?

Community
  • 1
  • 1
joshua.paling
  • 13,762
  • 4
  • 45
  • 60