Access methods defined in helper


#1

Hey

How do I access methods that I have defined in helpers?


Helper id_browser_helper.rb:

module IdBrowserHelper
def table_identities(message)
render_text message
end
end


Controller id_browser_controller.rb:

class IdBrowserController < ApplicationController
def view_genre()
table_identities(‘identities_box’)
end
end


This is the error message I get:
NoMethodError in Id_browser#view_genre
undefined method `table_identities’ for #IdBrowserController:0x385add0

What should I do? i have tried to require the helper file in the
controller, but that didn’t help.


#2

Am Sonntag, den 05.03.2006, 12:14 +0100 schrieb last resort:


NoMethodError in Id_browser#view_genre
undefined method `table_identities’ for #IdBrowserController:0x385add0

What should I do? i have tried to require the helper file in the
controller, but that didn’t help.

Helper methods in helper files hold code outsourced from your view, not
your controller, to make code simpler or reusable.

If you want to abstract code in your controllers, you have two choices:

  1. Put your common methods to ApplicationController
    (app/controllers/application.rb), so you can use them in every
    controller in your application. Don’t forget to make them private, so
    they could not be called as actions.

  2. Organize it in a module and include it in the controller where you
    need the methods.

Example:

lib/login.rb

module Login
def current_user
# some code to find current user
end
end

app/controllers/comments_controller.rb

class CommentsController < ApplicationController
include Login

def edit
user = current_user
end
end


Norman T.

http://blog.inlet-media.de