Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 275
von spotz Avatar asked Mar 28 '26 04:03

von spotz


1 Answers

<% 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 %>

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!