Ruby on Rails/ActionController/Actions
Introduction[edit | edit source]
Now that you worked your way though Models and Views, it's time to get to the last part of MVC: the Controller. Your controller creates the proper data for the view and handles some logic structures. In order to fully understand the whole framework, you should also read about "Routing" to fully understand how things work together.
Actions[edit | edit source]
Actions are methods of your controller which respond to requests. For example:
class PeopleController < ApplicationController def index @people = Person.find(:all) end def show @people= Person.find(params[:id]) end end
In this example the people controller has two actions: index and show. The action called index is the default action which is executed if no action is specified in the URL. For example:
The index action can also be called explicitly:
The show action must be called explicitly unless a route has been set up (routing will be covered later):
In the example above the number 1 will be available in params[:id]. This is because the default route is:
This indicates that the first part of the path is the controller name, the second part the action name and the third part is the ID. A detailed explanation of routes is provided in its own chapter.