Rails and Partials

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
[email protected]

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs