ruby-on-railstemplatesswitch-statementerb

Case statement doesn't work in erb partial


I have need for a conditional control structure in the view to build the form. (Please don't ask why I don't use helper methods like form_with. I can sure make use of some helper functions. But with my problem Rails obviously exiges me to build the form myself over a large part) Well, where were we? Yes, I need to make use of a conditional control structure in an erb.html template. I try a case statement, but Rails doesn't seem to like this.

<% case var_query_method %>

  <% when "text_field" %>

  <p>
    <%= label @node.class.name.underscore.to_sym, :text %><br>
    <%= text_field @node.class.name.underscore.to_sym, :text %>
  </p>


<% end %>

What am I doing wrong?


Solution

  • <% case var_query_method 
               when "text_field" %>
             <p>
                <%= label @node.class.name.underscore.to_sym, :text %><br>
                <%= text_field @node.class.name.underscore.to_sym, :text %>
            </p>
            <% end %>
    

    if you need add another when

    <% case var_query_method 
               when "text_field" %>
             <p>
                <%= label @node.class.name.underscore.to_sym, :text %><br>
                <%= text_field @node.class.name.underscore.to_sym, :text %>
            </p>
           <% when "no" %>
                <p>hello another when </p>
            <% end %>