Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you route an action to the application controller in Rails 3?

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.

like image 769
Hard-Boiled Wonderland Avatar asked Jan 24 '11 03:01

Hard-Boiled Wonderland


People also ask

How do routes work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

What is application controller in Rails?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.

What is action controller Rails?

Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either it renders a template or redirects to another action.


2 Answers

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.

like image 196
Pavling Avatar answered Sep 27 '22 22:09

Pavling


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

like image 41
dnch Avatar answered Sep 27 '22 20:09

dnch