Forum: Ruby on Rails Returning a code chunk from an action

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.
doug (Guest)
on 2007-03-04 05:23
(Received via mailing list)
Hi --

I'm very new to Rails.  I'm using a simple project to get acquainted.
In this project what it seems to me that I want to do is to have an
action return a chunk of dynamically-generated html code complete with
dynamic content, html tags, etc.  Of course, it is intended that this
content will then be incorporated into one or more views.  I would
think that this would be a very common need.  Since I'm not really
finding anything on this, I would first like to know if
I'm heading off in the wrong direction.  If what I want to do is
reasonable; then, I'd like to know how to do it.  I could easily
develop the dynamic content and send it to the console
with a series of puts statements at various strategic locations in my
action.  However, I don't want to do that.  I want to RETURN the
dynamic code so that I can incorporate it
into views.  If this is something that is commonly done, could someone
please square me
away on the proper methodology for handling it?  Thanks for any input.

       ... doug
Tom F. (Guest)
on 2007-03-04 06:47
(Received via mailing list)
Your way of working goes against how Rails works, and when you do that
you'll find Rails working against you rather than with you.

Here's how I would achieve what you want:

1. In the controller, do the logic you need to setup for generating
your HTML - whatever that logic is
2. In the view code, you will take what variables you setup in the
controller, and use those to generate the HTML

A simple example - Display the current user's name:

Your way:

1. In the controller, find the current user and build some HTML -
@username_html = "<b>username</b>"
2. In the view, put the username HTML on the page: <%= @username_html

The Rails way:
1. Controller code: get the current user into @user - @user =
2. View code, display the user's name: <b><% %></b>

If your view logic starts to get complex (more than simple loops and
if cases), you can build a helper method (in the _helper.rb file) to
make the code easier to work with.

The key is to keep code that controls what's going on in your
Controller, and code that generates what the user sees in your Views.
doug (Guest)
on 2007-03-04 22:22
(Received via mailing list)
Thanks, Tom, for your very helpful post.  It's obvious that
I am not yet thinking in Rails.  Once I get my mind wrapped
around it I'll be OK.  Your post was very helpful because it
addressed philosophically where I was straying from the
beaten path.  I just need to get into the Rails way of doing
things and all will be well.  Thanks again.

     ... doug
Robert W. (Guest)
on 2007-03-05 23:00
(Received via mailing list)

Technically you should think about this, not from a "Rails" point of
view, but rather understand that Rails incorporates the Model View
Controller (MVC) architectural pattern.  When you understand that then
you will understand Rails implementation of MVC.

This topic is locked and can not be replied to.