CSC/ECE 517 Fall 2012/ch1b 1w64 nn

From PG_Wiki
Jump to: navigation, search

SaaS - 3.12 Controller and views

Coursera is a technology company that provides free online education by making videos. SaaS is one such video series made by University of California, Berkley.



This article focuses on the concepts and usage of controllers and views in Rails application which is available as one of the SaaS video lectures. It gives an overview of MVC responsibilities in a Rails application with the main focus on the creation of controllers and views for a specific model.

MVC responsibilities

In Rails, applications are broken into three types of components: models, views, and controllers.

class Movie < ActiveRecord::Base
  belongs_to :genre  
  has_many :actor  

  def find_movie_name(movie_id)
    if movie_id then
      movie = Movie.find_by_id(movie_id)      
class MoviesController < ApplicationController
  def show
    @movie = Movie.find(params[:id])

Here, params is the parameter from the request that is parsed out by the routing subsystem. This contains the id of the movie that the user wants to find which can be fetched by calling Movie.find. In a real application, this is unsafe and must be handled by an exception since params[:id] can happen to be a non-existing value and will result in an error. Finally the instance variable @movie in the controller is going to be available to the view.

Adding a new controller action to Rails application

In Rails controller forms the logical center of the application. Controller is essentially a Ruby class is inherited from ActionController super class [3]. To create a controller, these steps must be followed.

class MoviesController < ApplicationController
  def new

Here the new function creates an instance of Movies controller.

match 'movies/new' => 'movies#new'
class MoviesController < ApplicationController
  def index
    @movies = Movie.all

The view for this controller will be found in app/view/movie/index.html.erb. It is shown below as follows:

<h1>Listing Movies</h1>
<% @movies.each do |movie| %>
    <td><%= movie.title %></td>
    <td><%= movie.description %></td>
    <td><%= link_to 'Show', movie_path(movie) %></td>
    <td><%= link_to 'Edit', edit_movie_path(movie) %></td>
    <td><%= link_to 'Remove', movie, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
<br />
<%= link_to 'New movie', new_movie_path %>

URI helpers

The routing subsystem that maps the URI to different controller actions also provides helper methods that generates routes in the context of that page. These routes generated by helper classes is used by the user in the views to navigate from one page to another [4]. In the above mentioned example of a view, there are three helper methods used namely, movie_path(movie), edit_movie_path(movie), new_movie_path. More examples are shown below in the table.

Helper method URI returned RESTful route Action
movies_path /movies GET /movies index
movies_path /movies POST /movies create
new_movie_path /movies/new GET /movies/new new
edit_movie_path(m) /movies/1/edit GET /movies/:id/edit edit
movie_path(m) /movies/1 GET /movies/:id show
movie_path(m) /movies/1 PUT /movies/:id update
movie_path(m) /movies/1 DELETE /movies/:id destroy

The following example illustrates how the helper methods work. Lets consider that the user is looking at the list of all movies index.html.erb. Somewhere on that page there will be a link whose argument is movie_path with an argument.

link_to movie_path(3)

movie_path is the helper that sets up and gives back a route to the show action for that movie id. So when the user clicks on it, the URI that is going to be generated is going to be movie/:id with a GET method because its a link. This is how it looks in the actual html.

<a href="/movies/3">...</a>

From here, once the URI is hit, the controller looks up for this URI in the routing subsystem. This corresponds to the Restful route GET /movies/:id which matches the show action of the movies controller. Further it takes :id and stores as params[:id]. With that params[:id], appropriate controller action is called.

Connecting URIs using helper methods

Further the user can return to the list of movies with the help of another URI helper, movies_path. movies_path with no arguments links to the index action. The line to add on show template is

link_to 'Back to List', movies_path

Here Back to List is the text that is click-able and movies_path is a method call.


Implementing MVC architecture in Rails application has few advantages. For example it separates models from presentation which in turn helps in implementing better test cases and better UI's for the user [9]. It also separates controller functionality with views which facilitates development of better web interfaces. Also Rails provides an easy way to implement MVC architecture for a user. All these advantages and the fact that MVC can be implemented in an effortless and uncomplicated manner necessitate the use of MVC architecture in Rails application.



Also see

Personal tools