R
R
ruby902015-02-04 22:52:01
ruby
ruby90, 2015-02-04 22:52:01

Why is the code section ignored in the erb template?

The code:

<td><% if @statistics[service].has_key?(:errorsrate)
          errorsrate = @statistics[service][:errorsrate].to_i
        else
          errorsrate = "-"  %><%=
            if errorsrate.is_a?(String)
              if errorsrate > 5
                'class="red"' 
              else 
                'class="green"'
              end  
            end %>
        <%end%> >
        <%= errorsrate %> </td>

I expect it to be like this:
<td class="green">
        8</td>

And it turns out like this:
<td
  >
        8 </td>

In this particular case, errorsrate is definitely not a string.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Viktor Vsk, 2015-02-05
@ruby90

Okay markup. But where is the logic?
By your code to get

<td class="green">
        8</td>

it is necessary that:
1) it @statistics[service]did NOT have the errorsrate key
2) it was a String
3) it was more than 5 (contradicts condition 2)
Believe me, there is NOT a String, otherwise there would be an error
(Try to compare "-" > 5 in the console)
In general, in fact, the trouble is not with the formatting, but with the description of the task. It looks like a laboratory at the university - a = x + 1 - b ...
What do you need to do?

E
Eugene Burmakin, 2015-02-04
@Freika

Firstly, the use of logic in view templates falls under "wrong".
Secondly, the formatting is wildly simple, but this should not affect the work.
Thirdly, and most likely, the fact is that your second one if
is in the interpretive erb tag: <%= %>
A should be<% %>

R
ruby90, 2015-02-05
@ruby90

Exactly. Victor, thank you so much for "blurring" my eyes. A very obvious mistake, really.

<td 
       <% if @statistics[ntag].has_key?(:errorsrate)
          errorsrate = @statistics[ntag][:errorsrate].to_i
        else
          errorsrate = "-"  
        end%>
        <%=
        if !errorsrate.is_a?(String)
          if errorsrate > 5
            'class="red"' 
          else 
            'class="green"'
          end  
        end %>>
        <%= errorsrate %> </td>

It works, despite the wild formatting, it would be nice to understand how to make it less wild.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question