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: <tr><td> <b>ABC</b></td><td> <%=h @my_result.abc -%> </td><td> <%=h @my_result.abcf %></td></tr> 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 <snip /> -- 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. -- Posted via http://www.ruby-forum.com/.
Pratik wrote:> Rendering a collection of partials > > http://api.rubyonrails.com/classes/ActionView/Partials.html > > <snip /> >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.
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.
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 Sindhwani <mo_mail@onghu.com> wrote:> Pratik wrote: > > Rendering a collection of partials > > > > http://api.rubyonrails.com/classes/ActionView/Partials.html > > > > <snip /> > > > 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. > >-- rm -rf / 2>/dev/null - http://null.in "Things do not happen. Things are made to happen." - JFK
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 ? > > -PratikHi 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": <td>ABC</td> <td><%= @results.abc %></td> <td><%= @results.abcf %></td> <td>XYZ</td> <td><%= @results.xyz %></td> <td><%= @results.xyzf %></td> 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.
> 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": > <td>ABC</td> <td><%= @results.abc %></td> <td><%= @results.abcf %></td> > <td>XYZ</td> <td><%= @results.xyz %></td> <td><%= @results.xyzf %></td> > > 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| %> <td><%= e.capitalize %></td> <td><%= @results[e] %></td> <td><%= @results[e + ''f''] %></td> <% end %> Yeah, you have to list them once, but it also lets you omit ones and sort them if you want.
Mohit Sindhwani wrote:> 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": > <td>ABC</td> <td><%= @results.abc %></td> <td><%= @results.abcf %></td> > <td>XYZ</td> <td><%= @results.xyz %></td> <td><%= @results.xyzf %></td> > > 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. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >You could do something like: <% %w{ abc xyz }.each do |field| %> <td><%= @results.send(field) %></td> <td><%= @results.send(field + ''f'') %></td> <% end %> But 18 pairs of fields really smells like it should be a seperate model with a has_many relationship. -- Jack Christensen jackc@hylesanderson.edu
In fact, you can define this array in the model itself and freeze it. But this would be violating the DRY principle.> <% [''abc'', ''xyz''].each do |e| %> > <td><%= e.capitalize %></td> <td><%= @results[e] %></td> <td><%= @results[e + ''f''] %></td> > <% end %> > > Yeah, you have to list them once, but it also lets you omit ones and sort > them if you want. >-- rm -rf / 2>/dev/null - http://null.in "Things do not happen. Things are made to happen." - JFK
Pratik wrote:> In fact, you can define this array in the model itself and freeze it. > > But this would be violating the DRY principle. > >> <% [''abc'', ''xyz''].each do |e| %> >> <td><%= e.capitalize %></td> <td><%= @results[e] %></td> >> <td><%= @results[e + ''f''] %></td> >> <% end %> >> >> Yeah, you have to list them once, but it also lets you omit ones and >> sort >> them if you want. >>Hi Pratik, Jack & Philip... Thanks for the suggestions - let me see what I can get working :) Actually, I''m thinking of having one array in the model - cos I need output the reference values for each of the fields... and I''m undecided yet if I should store it as a field in the database or just as a static array in the database. Cheers Mohit.
> You could do something like: > > <% %w{ abc xyz }.each do |field| %> > <td><%= @results.send(field) %></td> > <td><%= @results.send(field + ''f'') %></td> > <% 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 :) 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.
Does anyone have an example rails app that connects to a subversion client? Or WebDAV? Danilo Gurovich Principal Engineer, Value Added Services EarthLink 2947 Bradley Drive Pasadena, CA 91107 626.296.5805 ? ofc 818.554.4022 ? mob ?You know, I have one simple request, and that is to have sharks with frickin'' laser beams attached to their heads!?