Headline

Web programming in Language:Ruby with Technology:Ruby on Rails

Characteristics

We exercise Web programming using Technology:Ruby on Rails. That is, we use the MVC pattern for the companies model in the scope of a dynamically typed language. We make use of several framework tools for generating code and migration.

Illustration

Most of the code was generated using the rails generate scaffold command. This generates

  • a model for the given class and attributes
  • a view including pages for editing, indexing, and showing instances
  • a controller for actions like creating, updating, and removing instances
Then, we used rake db:migrate to migrate the tables in the underlining database.

In the next section, we will illustrate how routing rules and resources are defined.

Routes

The routes.rb file defines a default route when entering the application:

get "home/index"

That is, when entering the URL "http://localhost:3000/", the user is redirected to the specified page.

This file also defines Ruby resources:

resources :employees

resources :departments do
  resources :departments
  resources :employees
end 

resources :companies do
  resources :departments
end

These rules define what kind of resources the application contains and how these resources should be nested. For instance, a company resource has many department resources.

The framework also provides routes for both index pages and pages for showing a concrete instance. For companies these helpers are provided as follows:

companies path
provides the URL for the index page for all companies.
company path(someCompany)
provides the URL for the given company.

This example also shows the use of pluralisation for name conventions.

In the next section we will continue by illustration the binding between models and views.

Model/View binding

We will illustrate this topic by showing how one can add an employee to an existing department using the edit page for departments.

In the edit.html.erb template file for departments we define a form for adding an employee:

Invalid Language supplied

We use

form for
to generate a form for a new employee. Submitting this form sends a request including the new employee in JSON and the department id of the owning department. This behavior is caused by the fact that we added employees as a nested resource of departments.

In the form

f
we then bind the name of the new employee to a textfield and the
isManager
property to a checkbox.

Submitting this form will call the employee controller's

create
method:

def create
      @department = Department.find(params[:department id])
      @employee = @department.employees.create(params[:employee])
      redirect to department path(@department)
  end

This method starts by searching the department instance by the given id. It then calls the

create
method of the employees collection of this department. Finally the method redirects the user to this department.

Architecture

The MVC code for this implementation can be found in this!!companies/app. The models folder holds models for companies, departments and employees. The controllers folder holds the controllers for actions like indexing, creating, updating and destroying of instances. The views folder contains the views for all classes including editing, showing and indexing views.

The schema for the underlining database can be found in schema.rb. The migration files in the migrate folder.

The routing rules can be found in routes.rb.

Usage

  • Run rails s in the project's root folder.
  • Run rake db:migrate in the project's root folder

There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.