Hi,
Can anyone please help me. What I want to do here is to print the label
and text field of this part of the table 5 times. The output I expected
to get is this:
TEXT NUMBER 0 (this is where the TEXT_FIELD will appear)
TEXT NUMBER 1 (this is where the TEXT_FIELD will appear)
.
.
.
TEXT NUMBER 5 (this is where the TEXT_FIELD will appear)
Hi,
Can anyone please help me. What I need to do here is to print the label
and text field of this part of the table 5 times. The output I expected
to get is this:
TEXT NUMBER 0 (this is where the TEXT_FIELD will appear)
TEXT NUMBER 1 (this is where the TEXT_FIELD will appear)
.
.
.
TEXT NUMBER 5 (this is where the TEXT_FIELD will appear)
Instead, what I get is:
TEXT NUMBER #{num} (this is where the TEXT_FIELD will appear)
TEXT NUMBER #{num} (this is where the TEXT_FIELD will appear)
.
.
.
TEXT NUMBER #{num} (this is where the TEXT_FIELD will appear)
Yup I don’t understand what your talking about. Can anyone please
explain?
Thanks,
user splash (I am a beginner)
It’s rather confusing if you post with 2 different identities (seems
odd that both you and fries88 have exactly the same question).
Anyway, go back to where you first read about rhtml templates and read
about the difference between <% and <%=
#{} only works when ruby evaluates a string, which isn’t what is
happening here.
I think the only problem is the TEXT NUMBER “#{num}” line. Where
you’ve got #{num} here isn’t being interpreted by ruby because you’ve
not wrapped it with an <% %> to tell ruby to take a look at it.
Really you don’t need the “#{num}” all you should need to do is TEXT
NUMBER <%= num %>.
Just fyi, using a for loop as you are is a little slower then using
either the times or upto… as far as why it is slower I’m not really
sure, but times and upto are definitely the “ruby way”…
e.g.
<% 5.times do |num| %>
… TEXT NUMBER <%= num %> …
<% end %>
or
<% 0.upto 4 do |num| %>
… TEXT NUMBER <%= num %> …
<% end %>
The upto form has the added benefit that you can start from an
arbitrary place (like 1 if you wanted to be 1 based instead of zero
based) and go through what your ended number should be.