Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails if statement with boolean on index.html.erb page

Maybe I'm missing something simple but I can't figure out after some time looking at this.

I want to check if a boolean is true in a database on a form, if it is display a up arrow, if not down arrow.

I have this

<% for probe in @probes %>
    <tr id="<%= cycle('list-line-odd', 'list-line-even') %>">
      <td>
        <%= if probe.online = true %>
        <%= image_tag("online-icon.png", :alt => "Online") %>           
        <%= end %>
    </td>
      <td><%= link_to probe.probe_name, probe %></td>
    </tr>
  <% end %>

but it's coming back this this error

compile error

syntax error, unexpected ')', expecting kTHEN or ':' or '\n' or ';'

@output_buffer.concat "\t      \t"; @output_buffer.concat(( if probe.online = true ).to_s);
@output_buffer.concat "\n"

syntax error, unexpected kEND
@output_buffer.concat "      \t \t"; @output_buffer.concat(( end ).to_s);@output_buffer.concat "\n"

with arrows pointing at the .to_s

like image 939
Ryan Avatar asked Jul 29 '09 15:07

Ryan


1 Answers

Firstly, you test for equality with ==, not =, which is the assignment operator.

Second — and this is what the error is complaining about — you need to use just plain <% rather than <%= with an if-statement. The latter form tries to turn the code inside it into a string, and of course it's meaningless to write (if something == true).to_s — there's no possible string value for that.

like image 141
Chuck Avatar answered Nov 01 '22 10:11

Chuck