Forum: Ruby on Rails access methods defined in helper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6738e5c4f17822f7e163ba938344755d?d=identicon&s=25 last resort (Guest)
on 2006-03-05 12:14
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.
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-05 13:30
(Received via mailing list)
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 Timmler

http://blog.inlet-media.de
This topic is locked and can not be replied to.