Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.0: Adding new action to a controller

Before rails 3.0, adding a new action to a controller was easy.

You merely add a new method foobar to the controller class (called mycontroller). Add an html file in the views folder for that controller, foobar.html.erb

Then, if you point the browser to .../mycontroller/foobar everything worked.

However, in rails 3.0 when i added a new action as described above, i get the following error:

No route matches "/mycontroller/foobar"

What changed in rails 3.0? What am i doing wrong?

like image 911
ARI Avatar asked Dec 18 '10 07:12

ARI


People also ask

What does controller do 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 does before action do in Rails?

If a "before" filter renders or redirects, the action will not run.


1 Answers

Add this to routes.rb:

get 'mycontroller/foobar'

This will route the URL http://mysite.com/foobar to the foobar action using HTTP GET.

Some more info:

  1. Note that defining a def foobar in the controller is not a strict requirement (unless you need to do something in foobar before the view is displayed) - but the view must exist. In other words, even if def foobar method does not exist in the controller, the view foobar.html.erb will still be rendered.

  2. Here is a good overview of routes in Rails 3.

  3. Also, in case you don't already know, you can list all the routes that you app knows about using rake routes. Consequently, if the output of rake routes does not list the route to some controller/action, then the 'No route matches' error will occur.

like image 128
Zabba Avatar answered Sep 30 '22 14:09

Zabba