For complex use cases we may want to pass data to views in order to present it to our users. Hanami puts emphasis on explicitness: data isn't shared between the controller action and the view unless we tell it to do so.

We use a simple and powerful mechanism to achieve our goal: exposures. Exposures create a getter on the action for the given name(s) and only the whitelisted instance variables are made available to the corresponding view.

# apps/web/controllers/dashboard/index.rb
module Web::Controllers::Dashboard
  class Index
    include Web::Action
    expose :greeting

    def call(params)
      @greeting = "Hello"
      @foo      = 23

In the example above we have exposed :greeting, but not :foo. Only greeting can be used from the view and template.

# apps/web/views/dashboard/index.rb
module Web::Views::Dashboard
  class Index
    include Web::View

    def welcome_message
      greeting + " and welcome"

If we try to use foo, Ruby will raise a NoMethodError.

Prev: Actions - Request & Response
Next: Actions - Rack Integration
Looking for Lotus? We renamed the project and it's now called Hanami. Read the announcement.