Home arrow Ruby-on-Rails arrow Page 3 - Web Development: Ruby on Rails

Web Development: Ruby on Rails

Ruby on Rails has grown enormously in popularity over the last couple of years. This article, the first of a six-part series, will introduce you to its principles and show you how to use it. It is excerpted from chapter 15 of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Copyright 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 14
March 22, 2007
  1. · Web Development: Ruby on Rails
  2. · 15.1 Writing a Simple Rails Application to Show System Status
  3. · 15.2 Passing Data from the Controller to the View
  4. · 15.3 Creating a Layout for Your Header and Footer

print this article

Web Development: Ruby on Rails - 15.2 Passing Data from the Controller to the View
(Page 3 of 4 )


You want to pass data between a controller and its views.


The view is an ERB template that is interpreted within the context of its controller object. A view cannot call any of the controller's methods, but it can access the controller's instance variables. To pass data to the view, set an instance variable of the controller.

Here's a NovelController class, to be put into app/controllers/novel_controller.rb. You can generate stubs for it by running script/generate controller novel index.

  class NovelController < ApplicationController
def index
      @title = 'Shattered View: A Novel on Rails'
      one_plus_one = 1 + 1
      increment_counter one_plus_one

    def helper_method
      @help_message = "I see you've come to me for help."


    def increment_counter(by)
      @counter ||= 0
      @counter += by

Since this is the Novel controller and the index action, the corresponding view is in app/views/novel/index.rhtml.

  <h1><%= @title %></h1>

  <p>I looked up, but saw only the number <%= @counter %>.</p>

  <p>"What are you doing here?" I asked sharply. "Was it <%=
  @counter.succ %> who sent you?"</p>

The view is interpreted after NovelController#index is run. Here's what the view can and can't access:

  1. It can access the instance variables @title and @counter, because they've been defined on the NovelController object by the time NovelController#index finishes running.
  2. It can call instance methods of the instance variables @title and @counter.
  3. It cannot access the instance variable @help_message, because that variable is defined by the method helper_method, which never gets called.
  4. It cannot access the variable one_plus_one, because that's not an instance variable: it's local to the index method.
  5. Even though it runs in the context of NovelController, it cannot call any method of NovelController--neither helper_method nor set_another_variable. Nor can it call index again.


The action method of a controller is responsible for creating and storing (in instance variables) all the objects the view will need to do its job. These variables might be as simple as strings, or they might be complex helper classes. Either way, most of your application's logic should be in the controller. It's okay to do things in the view like iterate over data structures, but most of the work should happen in the controller or in one of the objects it exposes through an instance variable.

Rails instantiates a new NovelController object for every request. This means you can't persist data between requests by putting it in controller instance variables. No matter how many times you reload the page, the @counter variable will never be more than two. Every time increment_counter is called, it's called on a brand new NovelController object.

Like any Ruby class, a Rails controller can define class variables and constants, but they will not be available to the view. Consider a NovelController that looks like this:

  class NovelController < ApplicationController
    @@numbers = [1, 2, 3]
    TITLE = 'Revenge of the Counting Numbers'

Neither @@numbers nor TITLE are accessible from within any of this controller's views. They can only be used by the controller methods.

However, contants defined outside of the context of a controller are accessible to every view. This is useful if you want to declare the web site's name in one easy-to-change location. The config/environment.rb file is a good place to define these constants:

  # config/environment.rb
  AUTHOR = 'Lucas Carlson'

It is almost always a bad idea to use global variables in object-oriented programming. But Ruby does have them, and a global variable will be available to any view once it's been defined. They will be universally available whether they were defined within the scope of the action, the controller, or outside of any scope.

  $one = 1
  class NovelController < ApplicationController
$two = 2
def sequel
$three = 3

Here's a view, sequel.rhtml, that uses those three global variables:

  Here they come, the counting numbers, <%= $one %>, <%= $two %>, <%= $three %>.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials