ARI
ARI

Reputation: 101

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?

Upvotes: 10

Views: 14902

Answers (2)

Zabba
Zabba

Reputation: 65457

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.

Upvotes: 19

Vijay Dev
Vijay Dev

Reputation: 27466

The error says it all. You have no corresponding route in your config/routes.rb. Check if the default route is commented out. If so, you would need to add a route for your new action.

Upvotes: 0

Related Questions