Class: Merb::Controller

All of your web controllers will inherit from Merb::Controller. This superclass takes care of parsing the incoming headers and body into params and cookies and headers. If the request is a file upload it will stream it into a tempfile and pass in the filename and tempfile object to your controller via params. It also parses the ?query=string and puts that into params as well.

Sessions

Session data can be accessed through the session hash:

  session[:user_id] = @user.id

Session data is available until the user‘s cookie gets deleted/expires, or until your specific session store expires the data.

Session Store

The session store is set in Merb.root/config/merb.yml :

  :session_store: your_store

Out of the box merb supports three session stores

cookie:All data (max 4kb) stored directly in cookie. Data integrity is checked on each request to prevent tampering. (Merb::CookieStore)
memory:Data stored in a class in memory. (Merb::MemorySession)
mem_cache:Data stored in mem_cache. (Merb::MemCacheSession)

See the documentation on each session store for more information.

You can also use a session store provided by a plugin. For instance, if you have DataMapper you can set

  :session_store: datamapper

In this case session data will be stored in the database, as defined by the merb_datamapper plugin. Similar functionality exists for activerecord and sequel currently.

Public Class Methods


build (request, response = StringIO.new, status=200, headers={'Content-Type' => 'text/html; charset=utf-8'})

    # File lib/merb/controller.rb, line 77
77:       def build(request, response = StringIO.new, status=200, headers={'Content-Type' => 'text/html; charset=utf-8'})
78:         cont = new
79:         cont.set_dispatch_variables(request, response, status, headers)
80:         cont
81:       end

callable_actions ()

    # File lib/merb/controller.rb, line 59
59:       def callable_actions
60:         @callable_actions ||= begin
61:           hsh = {}
62:           (public_instance_methods - hidden_actions).each {|action| hsh[action.to_s] = true}
63:           hsh
64:         end
65:       end

hidden_actions ()

    # File lib/merb/controller.rb, line 67
67:       def hidden_actions
68:         write_inheritable_attribute(:hidden_actions, Merb::Controller.public_instance_methods) unless read_inheritable_attribute(:hidden_actions)
69:         read_inheritable_attribute(:hidden_actions)
70:       end

hide_action (*names)

Hide each of the given methods from being callable as actions.

    # File lib/merb/controller.rb, line 73
73:       def hide_action(*names)
74:         write_inheritable_attribute(:hidden_actions, hidden_actions | names.collect { |n| n.to_s })
75:       end

inherited (klass)

    # File lib/merb/controller.rb, line 54
54:       def inherited(klass)
55:         _subclasses << klass.to_s unless _subclasses.include?(klass.to_s)
56:         super
57:       end

Public Instance Methods


body ()

Accessor for @_body. Please use body and never @body directly.

     # File lib/merb/controller.rb, line 122
122:     def body
123:        @_body  
124:     end

cookies ()

     # File lib/merb/controller.rb, line 140
140:     def cookies
141:       request.cookies
142:     end

dispatch (action=:index)

     # File lib/merb/controller.rb, line 107
107:     def dispatch(action=:index)
108:       start = Time.now    
109:       if self.class.callable_actions[action.to_s]
110:         params[:action] ||= action
111:         setup_session
112:         super(action)
113:         finalize_session
114:       else
115:         raise ActionNotFound, "Action '#{action}' was not found in #{self.class}"
116:       end
117:       @_benchmarks[:action_time] = Time.now - start
118:       Merb.logger.info("Time spent in #{self.class}##{action} action: #{@_benchmarks[:action_time]} seconds")
119:     end

headers ()

Accessor for @_headers. Please use headers and never @_headers directly.

     # File lib/merb/controller.rb, line 145
145:     def headers
146:       @_headers
147:     end

params ()

     # File lib/merb/controller.rb, line 136
136:     def params
137:       request.params
138:     end

part (opts={})

Dispatches a PartController. Use like:

  <%= part TodoPart => :list %>

will instantiate a new TodoPart controller and call the :list action invoking the Part‘s before and after filters as part of the call.

returns a string containing the results of the Part controllers dispatch

You can compose parts easily as well, these two parts will stil be wrapped in the layout of the Foo controller:

class Foo < Application

  def some_action
    wrap_layout(part(TodoPart => :new) + part(TodoPart => :list))
  end

end

     # File lib/merb/controller.rb, line 201
201:     def part(opts={})
202:       res = opts.inject([]) do |memo,(klass,action)|
203:         memo << klass.new(self).dispatch(action)
204:       end
205:       res.size == 1 ? res[0] : res
206:     end

request ()

Accessor for @_request. Please use request and never @_request directly.

     # File lib/merb/controller.rb, line 132
132:     def request
133:        @_request
134:     end

response ()

Accessor for @_response. Please use response and never @_response directly.

     # File lib/merb/controller.rb, line 155
155:     def response
156:       @_response
157:     end

route ()

Accessor for @_route. Please use route and never @_route directly.

     # File lib/merb/controller.rb, line 160
160:     def route
161:       request.route
162:     end

send_mail (klass, method, mail_params, send_params = nil)

Sends mail via a MailController (a tutorial can be found in the MailController docs).

 send_mail FooMailer, :bar, :from => "foo@bar.com", :to => "baz@bat.com"

would send an email via the FooMailer‘s bar method.

The mail_params hash would be sent to the mailer, and includes items like from, to subject, and cc. See Merb::MailController#dispatch_and_deliver for more details.

The send_params hash would be sent to the MailController, and is available to methods in the MailController as params. If you do not send any send_params, this controller‘s params will be available to the MailController as params

     # File lib/merb/controller.rb, line 179
179:     def send_mail(klass, method, mail_params, send_params = nil)
180:       klass.new(send_params || params, self).dispatch_and_deliver(method, mail_params)
181:     end

session ()

Accessor for @_session. Please use session and never @_session directly.

     # File lib/merb/controller.rb, line 150
150:     def session
151:       request.session
152:     end

set_dispatch_variables (request, response, status, headers)

     # File lib/merb/controller.rb, line 84
 84:     def set_dispatch_variables(request, response, status, headers)
 85:       if request.params.key?(_session_id_key)
 86:         if Merb::Config[:session_id_cookie_only]
 87:           # This condition allows for certain controller/action paths to allow
 88:           # a session ID to be passed in a query string. This is needed for
 89:           # Flash Uploads to work since flash will not pass a Session Cookie
 90:           # Recommend running session.regenerate after any controller taking
 91:           # advantage of this in case someone is attempting a session fixation
 92:           # attack
 93:           if Merb::Config[:query_string_whitelist].include?("#{request.controller_name}/#{request.action}")
 94:           # FIXME to use routes not controller and action names -----^
 95:             request.cookies[_session_id_key] = request.params[_session_id_key]
 96:           end
 97:         else
 98:           request.cookies[_session_id_key] = request.params[_session_id_key]
 99:         end
100:       end
101:       @_request  = request
102:       @_response = response
103:       @_status   = status
104:       @_headers  = headers
105:     end

status ()

Accessor for @_status. Please use status and never @_status directly.

     # File lib/merb/controller.rb, line 127
127:     def status
128:        @_status
129:     end