I’m sorry - just couldn’t come up with a subject that describes the
problem
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 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…
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 %>
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
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.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.