Passing Data from the Controller to the View

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:

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 %>.

Категории