Forum: Ruby on Rails Best way to develop a self-contained widget ?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
876808a239de9b5f91db930286412176?d=identicon&s=25 Nicolas Jacobeus (jug)
on 2007-04-10 09:07

I am developing a small widget composed of several form elements, which
are connected with javascript code (i.e. what the user does on an
element has an impact on what another element shows).

At first I put the code in a function in my application helper, but as
the widget was evolving, I wanted to put it in a dedicated class, as
each instance of the widget needs some business logic. Indeed, sometimes
the widget has to be placed several times on a page, and the UI items
are not always beside each other (they could be on several rows of a
table), so each widget should have several attributes (such as the DOM
id of each of its elements) and several methods (e.g. to display each
part of the widget as needed).

So I made a class in the /lib directory, containing all the code of the
widget. This is elegant, but then I noticed that I cannot use ActionView
helper functions (such as select_tag or javascript_tag) from within that
class. I know that I could include all the needed ActionView::Helpers
modules in the class, but is it really the best way to do that ? How
would you develop such a reusable widget, composed of html elements,
javascript and some ruby logic ?

Thanks in advance,

This topic is locked and can not be replied to.