nivanka
nivanka

Reputation: 1372

using a helper method in the view rails 3

Hi I have a helper method, which returns a string, where i need to use this on every view. I am trying to add this method to the ApplicationHelper

my ApplicationController looks like this

class ApplicationController < ActionController::Base
  helper :application
end

my ApplicationHelper looks like this

module ApplicationHelper
  def ThemeDir
    "http://mysite.com/something/something"
  end
end

when i try to use this method in the view as

<%= ThemeDir %> 

it gives me the following error.

uninitialized constant ActionView::CompiledTemplates::ThemeDir

can someone help me with this please.

Upvotes: 1

Views: 5024

Answers (2)

Holger Just
Holger Just

Reputation: 55718

The convention in Ruby is that only constants (i.e. "classic" constants, classes and modules) start with an upper-case letter. Methods and variables should always start with a lower-case letter.

Also constants are written in camel case (notice the two "humps" in ApplicationController?) while variables and methods are generally be written in underscore syntax, e.g theme_dir. This distinguishes ruby from languages like Java, Javascript, or C# where camelcase is used everywhere (although they still distinguish constants/initializing functions from variables and functions via the case of the first letter).

The difference is important as methods are resolved differently from constants. So it's important for Ruby to know if the thing you are requesting is a constant or a method or local variable.

Concluding this, please name your helper method theme_dir and everything should be fine.

Upvotes: 9

Vik
Vik

Reputation: 5961

In Rails 'ThemeDir' treating it as constant .

Make the method name like 'theme_dir', and try that .

Upvotes: 5

Related Questions