Module: Camping::Helpers

Included in:
Defined in:


Helpers contains methods available in your controllers and views. You may add methods of your own to this module, including many helper methods from Rails. This is analogous to Rails’ ApplicationHelper module.

Using ActionPack Helpers

If you’d like to include helpers from Rails’ modules, you’ll need to look up the helper module in the Rails documentation at

For example, if you look up the ActionView::Helpers::FormTagHelper class, you’ll find that it’s loaded from the action_view/helpers/form_tag_helper.rb file. You’ll need to have the ActionPack gem installed for this to work.

Often the helpers depends on other helpers, so you would have to look up the dependencies too. FormTagHelper for instance required the content_tag provided by TagHelper.

require 'action_view/helpers/form_tag_helper'

module Nuts::Helpers
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::FormTagHelper

Return a response immediately

If you need to return a response inside a helper, you can use throw :halt.

module Nuts::Helpers
  def requires_login!
    unless @state.user_id
      redirect Login
      throw :halt

module Nuts::Controllers
  class Admin
    def get
      "Never gets here unless you're logged in"

Instance Method Summary collapse

Instance Method Details

#/(p) ⇒ Object

Simply builds a complete path from a path p within the app. If your application is mounted at /blog:

self / "/view/1"    #=> "/blog/view/1"
self / "styles.css" #=> "styles.css"
self / R(Edit, 1)   #=> "/blog/edit/1"

# File 'lib/camping-unabridged.rb', line 220

def / p;p[0]==?/?@root+p :p end

#R(c, *g) ⇒ Object

From inside your controllers and views, you will often need to figure out the route used to get to a certain controller c. Pass the controller class and any arguments into the R method, a string containing the route will be returned to you.

Assuming you have a specific route in an edit controller:

class Edit < R '/edit/(\d+)'

A specific route to the Edit controller can be built with:

R(Edit, 1)

Which outputs: /edit/1.

If a controller has many routes, the route will be selected if it is the first in the routing list to have the right number of arguments.

Using R in the View

Keep in mind that this route doesn’t include the root path. You will need to use / (the slash method above) in your controllers. Or, go ahead and use the Helpers#URL method to build a complete URL for a route.

However, in your views, the :href, :src and :action attributes automatically pass through the slash method, so you are encouraged to use R or URL in your views.

module Nuts::Views
  def menu! do
      a 'Home', :href => URL()
      a 'Profile', :href => "/profile"
      a 'Logout', :href => R(Logout)
      a 'Google', :href => ''

Let’s say the above example takes place inside an application mounted at http://localhost:3301/frodo and that a controller named Logout is assigned to route /logout. The HTML will come out as:

<div id="menu">
  <a href="http://localhost:3301/frodo/">Home</a>
  <a href="/frodo/profile">Profile</a>
  <a href="/frodo/logout">Logout</a>
  <a href="">Google</a>

# File 'lib/camping-unabridged.rb', line 202

def R c,*g;p,h=
/\(.+?\)/,g.grep(Hash);g-=h;raise"bad route"unless u=c.urls.find{|x|break x if
x.scan(p).size==g.size&&/^#{x}\/?$/=~(x=g.inject(x){|x,a|x.sub p,U.escape((a.
to_param rescue a))}.gsub(/\\(.)/){$1})};h.any?? u+"?"+U.build_query(h[0]):u

#URL(c = '/', *a) ⇒ Object

Builds a URL route to a controller or a path, returning a URI object. This way you’ll get the hostname and the port number, a complete URL.

You can use this to grab URLs for controllers using the R-style syntax. So, if your application is mounted at and you have a View controller which routes as R '/view/(\d+)':

URL(View,    #=> #<URL:>

Or you can use the direct path:

self.URL               #=> #<URL:>
self.URL + "view/12"   #=> #<URL:>
URL("/view/12")        #=> #<URL:>

It’s okay to pass URL strings through this method as well:

URL("")  #=> #<URL:>

Any string which doesn’t begin with a slash will pass through unscathed.

# File 'lib/camping-unabridged.rb', line 243

def URL c='/',*a;c=R(c,*a) if c.respond_to?(
:urls);c=self/c;c=@request.url[/.{8,}?(?=\/|$)/]+c if c[0]==?/;URI c end