Last Updated: February 25, 2016
·
342
· goedev001

Controller basics

The naming convention of controllers in Rails favors pluralization of the last word in the controller's name, although it is not strictly required (e.g. ApplicationController).

The controller naming convention differs from the naming convention of models, which expected to be named in singular form.

Methods

A method is created using the def statement. For example:

class ClientsController < ApplicationController
    def new
    end
end

As an example, if a user goes to /clients/new in your application to add a new client, Rails will create an instance of ClientsController and run the new method.

Parameters

You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data.

Rails does ** not ** make any distinction between query string parameters and POST parameters, and both are available in the params hash in your controller:

def create
    @client = Client.new(params[:client])
    if @client.save
        redirect_to @client
    else
        # This line overrides the default rendering behavior, which
        # would have been to render the "create" view.
        render "new"
    end
end

1 Response
Add your response

Wow, what is a parameter?

over 1 year ago ·