2

I am trying to check a condition and then include "Stop Server" and "Start Server" anchor tags. I am getting an error.

Please advise what to change. thanks.

<%= condition ? "Running" <a href='/stop'> Stop Server </a> :"Stopped" <a href='/start'> Start Server</a> %> </td>
Srujan Kumar Gulla
  • 5,721
  • 9
  • 48
  • 78

3 Answers3

7

You're mixing the code and html. It would be more clear to do something like this:

<% if condition %>
    "Running" <a href='/stop'> Stop Server </a>
<% else %>
    "Stopped" <a href='/start'> Start Server</a>
<% end %>
Matt
  • 20,108
  • 1
  • 57
  • 70
  • This is a good answer. But in addition, I'd add that a big part of the original problem is that the tags you're outputting (which from a Ruby perspective, are [or should be] strings) are not quoted at all. You could fix that, but that line very quickly becomes complicated and difficult to read and maintain. That's my Matt's answer is the way to go. – Kent Rancourt Jan 09 '14 at 20:04
2

Take a look at link_to_if

 <%= 
   link_to_if condition, 'Run server', run_server_path do
     link_to 'Stop server', stop_server_path
   end 
 %>
CodeGroover
  • 2,157
  • 19
  • 25
0

You should do something like this.

<%= condition ? "Running <a href='/stop'> Stop Server </a>" :"Stopped <a href='/start'> Start Server</a> %> </td>"

The anchor tag if by itself not a valid ruby statement and erb is all about dynamic views. Your ruby statement will become valid once you make it as a string which is what you are missing.

xvidun
  • 477
  • 5
  • 12