Ramaze Wiki
History
Table of Contents
  1. Features/Helpers
    1. Default Helpers
      1. CGI
      2. File
      3. Link
      4. Redirect
      5. Session/Flash
    2. Optional Helpers
      1. Aspect
      2. Auth
      3. Cache
      4. Identity
      5. Inform
      6. Markaby
      7. NitroForm
      8. OpenID
      9. Pager
      10. Partial
      11. Stack
    3. Custom Helpers

Features/Helpers

Default Helpers

CGI

Shortcuts for escaping and unescaping strings for the web.

File

Helps you serving files from your Controller in a fast way.

Provides easy ways to construct links to invoke your application's Controllers and Actions. The method r constructs a href to your controller actions.

AdminController.r(:users) # => /admin/users
r(:main) #=> /foo/main (if current controller is mapped to /foo)

You can also create full link tags with A:

Wiki.a(:home)                   # => '<a href="/wiki/home">home</a>'
Wiki.a('home', :home)           # => '<a href="/wiki/home">home</a>'
Wiki.a('home', :/)              # => '<a href="/wiki/">home</a>'
Wiki.a('foo', :/, :foo => :bar) # => '<a href="/wiki/?foo=bar">foo</a>'
Wiki.a('example', 'http://example.com') # => '<a href="http://example.com">example</a>'

Redirect

Easy redirection

Session/Flash

Store data on the session for the current and next request only. This is usually used to display temporary messages to the user after a redirect. In many cases it's considered good practice to redirect after a successful form POST, so the flash mechanism allows you to show the user a success message on the page they are redirected to.

# Go to / and a message is set, before redirecting to /welcome, which
# displays the message. Refresh /welcome and the message will disappear
# because flash data only lives for 1 extra request.
class MainController < Ramaze::Controller
  def index
    flash[:message] = 'Hello'
    redirect r(:welcome)
  end

  def welcome
    "#{flash[:message]}"
  end
end

Optional Helpers

Aspect

Allows you to wrap different Actions on your Controller with code

Auth


Simple way to add basic authentication. Check out the user helper for implementing authentication using a model. The auth example is another approach to authentication, without using a model.

Cache


Easy caching of actions and values.
Note that if there is flash data on the user's session, action caching is ignored for that request, because it is assumed that the flash data is intended to be used in the response.
This feature can be turned off by setting Ramaze::Global.nocacheflash = false.

class MainController < Ramaze::Controller
  helper :cache

  def time
    'Time of first access was: ' + (cache[:time] ||= Time.now)
  end

  def index
    "Hi world, it's #{Time.now} right now"
  end
  # cache output of index action
  cache :index

  def name
    'your name is ' + request['name']
  end
  # cache output of name for every unique name
  cache :name, :key => lambda{ request['name'] }

  def value
    'hi'
  end
  # cache for 60 seconds
  cache :value, :ttl => 60
end

Identity


For ease of use of the OpenID authentication mechanism

Inform


Wrapping the functionality of Ramazes logging facilities

Markaby


Allows you to use Markaby in your Controller without having it as the default templating engine

NitroForm


Hooks up on Nitro's form builder to help you creating forms from Og objects

OpenID


Authentication via OpenID made easy

Pager


Displays a collection of entitities in multiple pages

Partial


class MyController
  def index
  end
  def list
    plain = request['plain']
    "Hello World from List!  Plain List == #{plain}"
  end
end

<html>
  <head><title>Partial Render Index</title></head>
  <body>
    #{render_partial(r(:list), 'plain' => true)}
  </body>
</html>

Note that you can't use symbol keys for the options hash; they have to be string keys.

Stack


Allows you to use a call/answer mechanism for things like redirection to the site from which a user entered login form data

Custom Helpers


Ramaze makes it easy to write your own helpers, simply create a ''helper'' subdirectory in your app. For example, you could create an emoticon helper in ''helper/emoticon.rb''

module Ramaze
  module Helper
    module Emoticon
      def emoticonize text
        text.gsub('=)', '<img src="/smiley.jpg" />')
      end
    end
  end
end

To use the helper in your controller and templates:

class MainController < Ramaze::Controller
  helper :emoticon

  def index
    emoticonize 'hello world, =)'
  end
end

Or, to make the helper available to all the controllers in your app:

class Ramaze::Controller
  helper :emoticon
end