Passing Data from the Controller to the View

Table of contents:

Problem

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

Solution

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

Heres 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
	 end

	 def helper_method
	 @help_message = "I see youve come to me for help."
	 end

	 private

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

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

	

<%= @title %>

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

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


The view is interpreted after NovelController#index is run. Heres what the view can and can access:

  • It can access the instance variables @title and @counter, because theyve been defined on the NovelController object by the time NovelController#index finishes running.
  • It can call instance methods of the instance variables @title and @counter.
  • It cannot access the instance variable @help_message, because that variable is defined by the method helper_method, which never gets called.
  • It cannot access the variable one_plus_one, because thats not an instance variable: its local to the index method.
  • Even though it runs in the context of NovelController, it cannot call any method of NovelControllerneither helper_method nor set_another_variable. Nor can it call index again.

Discussion

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 applications logic should be in the controller. Its 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 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, its 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
	end

Neither @@numbers nor TITLE are accessible from within any of this controllers 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 sites 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 its 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
	 end
	end

Heres a view, sequel.rhtml, that uses those three global variables:

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



Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net