18

I am using the gem rails3-jquery-autocomplete and had no problems with it, however I have now moved my autocomplete form into the application template and therefore the ajax calls are now being dealt by the application controller, so my routes have changed from:

home/autocomplete_category_name

and now need to have the home removed and the path from:

home_autocomplete_category_name_path

to:

autocomplete_category_name_path

Anybody got any ideas? Still learning the ins and outs of Rails so this is a dead end for me right now.

Thanks.

Hard-Boiled Wonderland
  • 1,359
  • 3
  • 17
  • 32
  • It would be best to post the current RoR actual code for the route in question as well as the URL that you are targeting. – jschorr Jan 24 '11 at 03:44
  • Also, include your current routes by typing `rake routes` at the console in your root project directory. – raidfive Jan 24 '11 at 07:56

6 Answers6

34

Old post, but the accepted answer is wrong. Though the spirit of it is right - you should probably move your method to a more appropriate controller; but if you insist on having the method in application_controller.rb

# routes.rb
match '/some_route', to: 'application#some_route', as: :some_route

...will route to the 'some_route' method in application_controller.rb.

Pavling
  • 3,933
  • 1
  • 22
  • 25
8

URLs don't map directly to ApplicationController - only subclasses of it.

You need to move the call to autocomplete into another controller. The location of the form shouldn't make a difference, as long as you're passing the correct path when you define your text_field

dnch
  • 9,565
  • 2
  • 38
  • 41
7

Another necro but there is a more DRY friendly way to do this, compared to pavling's solution

get :autocomplete_category_name, controller:"application"

I was trying(yesterday) to move it to the Application controller to try to reuse code better and not tie it to a specific controller. Only today did I realize that makes no sense, since the controller used to answer this call is going to be a completely different object to the controller rendering the view anyway..

Karthik T
  • 31,456
  • 5
  • 68
  • 87
1

Using Rails 4, your have to use get, not match...

# routes.rb
get '/autocomplete_category_name', to: 'application#autocomplete_category_name', as: :autocomplete_category_name
maduhaime
  • 21
  • 3
0

try something like

match "home/autocomplete_category_name", "home#autocomplete_category_name", :as => "autocomplete_category_name"
kylewelsby
  • 4,031
  • 2
  • 29
  • 35
0

In Rails 4, use this:

get 'application/autocomplete_category_name', as: 'autocomplete_category_name'

You have 'autocomplete_category_name_path' to you links

Artem
  • 1