0

I have the friendly_id gem installed (version 5.1.0) and have implemented the steps in the documentation to extend my model, add the slug field to the table, etc. This url works as expected:

http://localhost:3000/owners/1

I'm trying to find by email address but look how the trailing .com is being treated:

http://localhost:3000/owners/me@example.com

{"id"=>"me@example",
 "format"=>"com"}

And since my client is really looking to receive the results of the find in json format, the client should be sending this URL to the server:

http://localhost:3000/me@example.com.json

but Rails doesn't like that:

No route matches [GET] "/owners/me@example.com.json"

where as

http://localhost:3000/owners/1.json

returns the json view as expected.

How (or what) should I code to get around this?

tobinjim
  • 1,862
  • 2
  • 19
  • 31

1 Answers1

0

Actually, It's a not a friendly_id problem. Your controller is not called at all because you have a URL (/owners/me@example.com/me@example.com) that doesn't satisfy any of your routes in config/routes.rb.

It's because of ".com" in the end of the path. You should add a constraint to tell Rails that you will have an email as a part of url. I've created a small project with the following routing:

get "/:email", :to => "application#test", :constraints => { :email => /.+@.+\..*/ }

And controller:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  def test
    respond_to do |format|
      format.any { render :json => { :hello => params[:email] } }
    end
  end
end

And after it I'm able to call it using curl:

curl -H "Accept: application/json" http://localhost:3000/boo@example.com

Result:

{"hello":"boo@example.com"}
kimrgrey
  • 562
  • 2
  • 10
  • Your exact setup works, so I've accepted the answer and express my thanks for the assistance you've given. When I try to have this action be caught by my owners controller Rails digs into the superclass and instead of looking up params[:email] it looks up params[:id] so I'll keep playing with this. – tobinjim Feb 04 '16 at 22:55
  • My bad: I had to alter the route to be get "owners/:email" etc. instead of just get "/:email" --- thanks so much!!! – tobinjim Feb 04 '16 at 23:00