Forum: Ruby on Rails Rendering objects directly via <%= render @some_object %>

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.
3dcb0c9fdc4e2b501c8177d91a5869a2?d=identicon&s=25 Heino H. Gehlsen (Guest)
on 2006-02-07 15:16
(Received via mailing list)
My original post might have been overlooked by most, but I've made a
plugin (link below) which allows rendering of objects directly via the
render method:

<%= render @some_object %> which would actuall render the @some_object
object (the object is even able to handle the options hash.

Rendering would actually be handeled by the object itself, by leaving it
up to the object to inteact with the template and parse the wished for
options hash back to the ActionView render method. This way the object
could descide to render nothing depending on internal state. The plugin
even allow the object being used as mere container of commonly used
:locals hashes set by the controller (as showed in the documentation).

An object could be made parseable anywhere, but the controller would be
an obvious place to start ;-)

class SomeClass

# Include renject functionality to allow object to render itself
include ActionView::Renjects::BaseRenject

# Generate hash to be parsed back to ActionView render method
# The options parameter, which contains the original options hash,
# could optionally be uesd to when generating the hash.
def renject_render_options( options = {} )

# Generate the wished for hash
return { :partial => @partial, :locals = {:foo => @foo, :baa => @baa }}

attr_writer :partial
attr_writer :foo
attr_writer :baa


@some_object = SomeClass
@some_object.partial = 'Partial' = 'Foo'
@some_object.baa = 'Baa'

Rendering @some_object via

<%= render @some_object %>

corresponds to

<%= render :partial => @partial, :locals=> {:foo => @foo, :baa => @baa}

Now imagine having to repeat this multiple times - and then trying to
find a typo...


The source and documentation is still avalible at

NB. Since my initial post, I have changed the classes into modules, so
that _any_ module could be rendered by extending these modules.

Kind regards
Heino H. Gehlsen
This topic is locked and can not be replied to.