# Rails and Partials

**URL:** <https://www.ruby-forum.com/t/rails-and-partials/133477>\
**Category:** Rails\
**Created:** [March 31, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/rails-and-partials/133477 "2008-03-31T23:26:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [March 31, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/rails-and-partials/133477/1 "2008-03-31T23:26:17Z")

</div>

Excuse my ignorance on partials but…

I am trying to use partials to create a uniform display box built with  
html tables. The only way I have thought about accomplishing this is to  
create two partials:

## \_start\_box.erb

| \<%= h(label\_text) %\> |
| --- |
| 
## \_end\_box.erb
 |

## The to use it my\_form.erb

%= render :partial =\> “start\_box”, :locals =\> { :label\_text =\> “Box  
Label”, :width =\> “97%” } %\>  
**Some Text**  
  
**Some More Text**  
  
**Some More Text**  
  
**Some More Text**  
\<%= render :partial =\> “\_end\_box” %\>

Then use some CSS to purdy it up.

This does seem to be a bit laborious when it seems partials are so  
flexible. Should I be using partials for this type of widget? Is their  
some way to accomplish this via a single partial instead of a partial  
for the first part of the table and a partial for the end part of the  
table?

Thanks  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [April 1, 2008, 12:50am UTC](https://www.ruby-forum.com/t/rails-and-partials/133477/2 "2008-04-01T00:50:51Z")

</div>

Steve Woolley wrote:

> This does seem to be a bit laborious when it seems partials are so  
> flexible. Should I be using partials for this type of widget? Is their  
> some way to accomplish this via a single partial instead of a partial  
> for the first part of the table and a partial for the end part of the  
> table?

Use a helper instead. Something like:

def tablize options = {}, &block

# Set some default values

options[:width] ||= ‘100%’  
options[:label] ||= ‘Box Label’  
header = content\_tag(:tr, content\_tag(:th, h(options[:label])))  
body = content\_tag :tr, content\_tag(:td, capture(&block))  
table = content\_tag :table, header+body,  
:id =\> :box, :width =\> options[:width]  
concat table, block.binding  
end

Then in your views you can do:

\<% tablize :width =\> ‘97%’, :label =\> ‘A Label’ do %\>  
**Some Text**  
  
**Some More Text**  
  
**Some More Text**  
  
**Some More Text**  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![interactivenyc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/85f322/32.png) [@interactivenyc](https://www.ruby-forum.com/u/interactivenyc)\
**Post date:** [April 1, 2008, 2:22am UTC](https://www.ruby-forum.com/t/rails-and-partials/133477/3 "2008-04-01T02:22:35Z")

</div>

Beautiful, beautiful, beautiful…

works like a champ. Thanks for your help!

Mark B. wrote:

> Use a helper instead. Something like:
> 
> def tablize options = {}, &block
> 
> # Set some default values
> 
> options[:width] ||= ‘100%’  
> options[:label] ||= ‘Box Label’  
> header = content\_tag(:tr, content\_tag(:th, h(options[:label])))  
> body = content\_tag :tr, content\_tag(:td, capture(&block))  
> table = content\_tag :table, header+body,  
> :id =\> :box, :width =\> options[:width]  
> concat table, block.binding  
> end
> 
> Then in your views you can do:
> 
> \<% tablize :width =\> ‘97%’, :label =\> ‘A Label’ do %\>  
> **Some Text**  
>   
> **Some More Text**  
>   
> **Some More Text**  
>   
> **Some More Text**  
> \<% end %\>
