# Returning a code chunk from an action

**URL:** <https://www.ruby-forum.com/t/returning-a-code-chunk-from-an-action/87591>\
**Category:** Rails\
**Created:** [March 4, 2007, 4:23am UTC](https://www.ruby-forum.com/t/returning-a-code-chunk-from-an-action/87591 "2007-03-04T04:23:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 4, 2007, 4:23am UTC](https://www.ruby-forum.com/t/returning-a-code-chunk-from-an-action/87591/1 "2007-03-04T04:23:22Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 4, 2007, 5:47am UTC](https://www.ruby-forum.com/t/returning-a-code-chunk-from-an-action/87591/2 "2007-03-04T05:47:27Z")

</div>

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 = “ **username** ”
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 =  
User.find(1)
2. View code, display the user’s name: **\<% =@user.name %\>**

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.

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 4, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/returning-a-code-chunk-from-an-action/87591/3 "2007-03-04T21:22:42Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![doug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dbc845/32.png) [@doug](https://www.ruby-forum.com/u/doug)\
**Post date:** [March 5, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/returning-a-code-chunk-from-an-action/87591/4 "2007-03-05T22:00:56Z")

</div>

Doug,

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.

See:

> **[Model–view–controller](https://en.wikipedia.org/wiki/Model-view-controller)**
>
> Model–view–controller (MVC) is a software design pattern commonly used for developing user interfaces that divides the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.
> Traditionally used for desktop graphical user interfaces (GUIs), this pattern became popular for designing web applications. Popular programming languages have MVC frameworks that facilita...
