html tag id from variable

I would like to create the id portion of a <td> tag using a string
variable. In my program i have a table that will have a varying number
of columns determined by a variable in the parameter file, $gItems. I
would like to be able to loop through each row labeling each column
based on its column number as shown below. The problem is i do not know
how to have the variable "item" read into the tag. Any solution or
other suggestion on how to solve this problem would be appreciated.

[code]
<table>
  <tr>
    <%for item in 0..$gItems%>
      <td id="col" + item.to_s >
         <%= "col" + item.to_s %>
      </td>
    <%end%>
  </tr>
</table>
[/code]

Ryan F. wrote in post #968899:

I would like to create the id portion of a <td> tag using a string
variable. In my program i have a table that will have a varying number
of columns determined by a variable in the parameter file, $gItems.

NO! NO! NO! NEVER USE GLOBAL VARIABLES FOR THIS SORT OF THING!

OK, now that I've got your attention :slight_smile: , please explain what you're
trying to do with $gItems. There will be a more appropriate place to
put it than in a global variable; we just have to figure out what it is.

I

would like to be able to loop through each row labeling each column
based on its column number as shown below. The problem is i do not know
how to have the variable "item" read into the tag. Any solution or
other suggestion on how to solve this problem would be appreciated.

You can do this the same way you've done it on the next line. ERb <%=
%> can go anywhere.

Note, however, that Haml is generally a superior alternative to ERb, and
in this case it provides an easier way to do this with its %tag[object]
syntax.

[code]
<table>
  <tr>
    <%for item in 0..$gItems%>
      <td id="col" + item.to_s >
         <%= "col" + item.to_s %>
      </td>
    <%end%>
  </tr>
</table>
[/code]

Best,

First thank you for your quick response. This forum seems like a place
I will be able to learn some proper rails coding.

NO! NO! NO! NEVER USE GLOBAL VARIABLES FOR THIS SORT OF THING!

I have never programmed in ruby and have inherited this application to
only make a few small changes. Unfortunately the original programmer
used 50+ global variables and I really do not want to rewrite
everything.

OK, now that I've got your attention :slight_smile: , please explain what you're
trying to do with $gItems. There will be a more appropriate place to
put it than in a global variable; we just have to figure out what it is.

this app has a parameter file where the number of items can be changed.
Depending on the integer that is placed in the parameter file, the view
must create a table element with the appropriate number of columns
($gItems). $gItems is created in ApplicationController class the first
time someone navigates to the Auctioneer view from the
auctioneer_controller class index method. This allows each subsequent
client screen to draw a table showing the appropriate number of items.
I feel like my explanation is very convoluted, as is the code. let me
know if i should break it down further.

One thing to note might be that this app is not database backed. This
is probably why the original programmer constantly used globals.

Note, however, that Haml is generally a superior alternative to ERb, and
in this case it provides an easier way to do this with its %tag[object]
syntax.

thanks, I like the look of Haml and will look into it further.

Ryan F. wrote in post #968910:

First thank you for your quick response. This forum seems like a place
I will be able to learn some proper rails coding.

NO! NO! NO! NEVER USE GLOBAL VARIABLES FOR THIS SORT OF THING!

I have never programmed in ruby and have inherited this application to
only make a few small changes. Unfortunately the original programmer
used 50+ global variables and I really do not want to rewrite
everything.

You may not have to, but it's usually a good idea to improve the parts
you're working on.

If the original programming is this bad, I assume there is little or no
test coverage? If that's the case, then I advise getting familiar with
RSpec and Cucumber, and writing tests for anything you have to change
(so you know you're not breaking things). Then do all new development
test-first.

OK, now that I've got your attention :slight_smile: , please explain what you're
trying to do with $gItems. There will be a more appropriate place to
put it than in a global variable; we just have to figure out what it is.

this app has a parameter file where the number of items can be changed.
Depending on the integer that is placed in the parameter file, the view
must create a table element with the appropriate number of columns
($gItems). $gItems is created in ApplicationController class the first
time someone navigates to the Auctioneer view from the
auctioneer_controller class index method. This allows each subsequent
client screen to draw a table showing the appropriate number of items.

That's dreadful -- and easy to fix. Read it from the config file as
before, but assign it to a class constant or something. See
http://railscasts.com/episodes/85-yaml-configuration-file for some
ideas.

I feel like my explanation is very convoluted, as is the code. let me
know if i should break it down further.

I *think* I understand...

One thing to note might be that this app is not database backed.

Then why is it using Rails in the first place, I wonder?

This
is probably why the original programmer constantly used globals.

Perhaps. The fact that the original programmer also used Hungarian
Notation and CamelCase suggests that he wasn't too familiar with Ruby
idioms at all...

Note, however, that Haml is generally a superior alternative to ERb, and
in this case it provides an easier way to do this with its %tag[object]
syntax.

thanks, I like the look of Haml and will look into it further.

Best,

hmmm... I'm going to say "sorry" in advance, in case this seems a
little harsh - it's not meant to be, but just my arm's length
evaluation of your situation.

Is it really a good solution for you to be making amendments to this
site? Given that the original developer obviously wasn't massively
proficient with Ruby/Rails idiom and good practice, and you confess to
being new to the language too, isn't it most likely that whatever
changes you make are going to increase the entropy of the system
rather than reduce it?

Are there no Ruby-experienced freelancers in your area you could
engage for a little help? If only to show you how to achieve what you
need doing, and check it after you've done it?

I raise the point, not to belittle your attempts at picking through
the code you've inherited (and let me guess... the original developer
didn't write many comments.... they never do!), but just from my
experience, you might regret increasing the complexity/spaghetti when
you look back at it after you have gained some more experience with
Ruby (it's such a beautiful language... and so many people butcher it
:-/

All the best,
Michael

PS
  <% for item in 0..$gItems %>
     <td id="col<%=h item.to_s %>">
       <%= "col" + item.to_s %>
     </td>
  <% end %>

blurgh! :wink:

Its not harsh...its the truth. I am having a lot of trouble
understanding the uniqueness of the rails language. To add to the
difficulty this is my first time working on a web app. I usually only
write socket based client/server apps, but I am going to look at this as
a chance to expand my knowledge. In all honesty i dont know if i would
ever use rails again because i have no need for the db and could just as
easily write the program in something im slightly (very slightly) more
familiar with like java.