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?
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.
If a "before" filter renders or redirects, the action will not run.
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:
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.
Here is a good overview of routes in Rails 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With