DRYing - similar named fields, etc

I’m sorry - just couldn’t come up with a subject that describes the
problem :frowning:

Anyway, this is my problem. I have a long list of fields that need to
be displayed when a ‘show’ is requested on the controller. I’m now
doing the list.rhtml and looking for a DRY way to do the following.

For each data item, I have 2 fields - one is the item name, the other is
a flag that describes the quality.
So, for item “ABC”, I have a data field “abc” and a flag field “abcf”
I want to display this as columns of the same row of the display table.
So, what I have is this:

ABC <%=h @my_result.abc -%> <%=h @my_result.abcf %> I'd rather not do this for each of the elements. Is there a DRY way to render this? The column name is all caps, the data field is all lowercase and the flag is the same thing all lowercase with 'f' after the name?

That’s the first question. Depending on how to do that, I may have
another question about conditional formatting on those fields. But,
I’ll hold back to see if I can figure that out based on what you guys
suggest for this.

Thanks
Mohit.

Rendering a collection of partials

http://api.rubyonrails.com/classes/ActionView/Partials.html


rm -rf / 2>/dev/null - http://null.in

“Things do not happen. Things are made to happen.” - JFK

You could look at the generated code from http://www.ajaxscaffold.com
for ideas.

I’m still not 100% clear about what you’re trying.

Have look at
http://rubyonrails.org/api/classes/ActiveRecord/Base.html#M000925

Is that something you’re looking for ?

-Pratik

On 7/25/06, Mohit S. [email protected] wrote:

posted. My understanding was that partials are to be used when


rm -rf / 2>/dev/null - http://null.in

“Things do not happen. Things are made to happen.” - JFK

Dr Nic wrote:

You could look at the generated code from http://www.ajaxscaffold.com
for ideas.

Thanks! I’ll take a look at that and see if it helps.
Cheers
Mohit.

Pratik wrote:

Rendering a collection of partials

Peak Obsession

Hi Pratik,

Thanks for replying. I did think of and look at partials before I
posted. My understanding was that partials are to be used when
rendering records. The basic partial template renders one record while
rendering a collection of partials will render an array of records

My problem is that I’m looking for a solution that allows me to render
pairs of fields within the same record…

Am I missing something?
Thanks
Mohit.

Mohit S. wrote:

ABC <%= @results.abc %> <%= @results.abcf %>

Cheers
Mohit.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

You could do something like:

<% %w{ abc xyz }.each do |field| %>

<%= @results.send(field) %> <%= @results.send(field + 'f') %> <% end %>

But 18 pairs of fields really smells like it should be a seperate model
with a has_many relationship.


Jack C.
[email protected]

Pratik wrote:

I’m still not 100% clear about what you’re trying.

Have look at
http://rubyonrails.org/api/classes/ActiveRecord/Base.html#M000925

Is that something you’re looking for ?

-Pratik
Hi Pratik,

Thanks for the email.

I guess my explanation wasn’t clear enough. I have pairs of fields, one
which is the field value, and the other which is a flag that defines the
quality of the value. I have about 18 pairs… the value field is
called (say) “abc” and the flag field is “abcf”…

Right now, I have 18 rows of formatting information in the “show”:

ABC <%= @results.abc %> <%= @results.abcf %> XYZ <%= @results.xyz %> <%= @results.xyzf %>

I was wondering if I could write a loop of some sort that would let me
avoid writing the formatting information as 18 separate rows.
Something elegant and DRY.

I’ll take a look at the link you just sent me - I just might be able to
use attribute names since it will create a sorted list… so the value
field (abc) and the flag field (abcf) will be back to back. I should be
able to iterate over that array… <% perhaps %>

Cheers
Mohit.

You could do something like:

<% %w{ abc xyz }.each do |field| %>

<%= @results.send(field) %> <%= @results.send(field + 'f') %> <% end %>

But 18 pairs of fields really smells like it should be a seperate
model with a has_many relationship.

Actually, this looks closest to what I was hoping for :slight_smile:

As for 1 pairs of fields, the data comes from a piece of equipment that
outputs all the data. Most fields are output by the equipment at every
instance. I have considered storing it as a has_many but since most
fields are output all the time, for now, it makes sense to leave it in
a single record that corresponds to the full data that was output.

Cheers
Mohit.

I was wondering if I could write a loop of some sort that would let me avoid
writing the formatting information as 18 separate rows. Something elegant and
DRY.

How about…

<% [‘abc’, ‘xyz’].each do |e| %>

<%= e.capitalize %> <%= @results[e] %> <%=
@results[e + ‘f’] %>
<% end %>

Yeah, you have to list them once, but it also lets you omit ones and
sort
them if you want.