views:

95

answers:

3

I am using rails and i need to know how i add a new action to my controller, im a beginner in rails so i dont really know if i just type "def action" in the controller.rb or is their more too it then that?

Thanks.

+3  A: 

Nope, that's it... simply adding a method to the controller class automatically creates a new action for that controller (at least with the default setup).

There's a little section about this in the Beginning Ruby on Rails book. Also, reading through the Ruby on Rails Getting Started Guide would probably be helpful. It demonstrates this.

fiXedd
+2  A: 

Well, it actually depends on how your routing is set up. If it falls through to the default route:

map.connect ':controller/:action/:id'

Then you have nothing else to do (see ActionController::Routing).

If you are using RESTful resources, you need to explicitly mention the action (and request type) in the routes.rb file (see ActionController::Resources).

Michael Sepcot
+1  A: 

As well as defining a new method in your controller, you will probably need to setup the view as well. By default the view corresponds to the controller and action name.

So controller main with action index looks for a template in:

views/main/index
Toby Hede