There are a lot of ways to do this, but an easy way would be to send an ajax GET request (on the change event for the autocomplete) with the id of the selected Foo
instance to a controller endpoint. Do a lookup with the id. If an instance with the id exists, return a 200; otherwise, return a 404. So you would have success and error handlers respectively to handle those cases.
At this point, you can do many more things. Your REST endpoint could send the result of foo_path(foo)
(remember you looked up foo
) in the body of the 200 response. You could keep a list on the client side of all the different foo_path(:id)
s corresponding to the choices in your autocomplete and pick the right one on success. Either way, you can then set document.location
in your JavaScript to the correct path.
And probably a lot of other things others will point out I'm sure.