Ruby on Rails/ActionController/Actions

Introduction

edit

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

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:

http://localhost:3000/people

The index action can also be called explicitly:

http://localhost:3000/people/index

The show action must be called explicitly unless a route has been set up (routing will be covered later):

http://localhost:3000/people/show/1

In the example above the number 1 will be available in params[:id]. This is because the default route is:

map.connect :controller/:action/:id

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.