Search code examples
ruby-on-railsbooleanbutton-to

How I create a non-visible button in rails using a boolean condition


So. I'm in a loop

<% products.each do |product|%>

      btn_hide = "btn-hide-class"

      <%= button_to "expired",
                    method_path(@path),
                    method: "get",
                    class: "btn btn-expired-supply #{btn_hide}",
                    remote: true
      %>
    </div>
  </div>
<% end %>

Also, in my CSS file I have

.btn-hide-class{
   display:none;
}

I don't want to do it

I want to do something like this, using a boolean condition for display this button:

<% products.each do |product|%>


      <%= button_to "expired",
                    method_path(@path),
                    method: "get",
                    **display: true**   
                    class: "btn btn-expired-supply",
                    remote: true
      %>
    </div>
  </div>
<% end %>

Do you know any solution for this?


Solution

  • Lets your products table has a boolean field named as 'expired' and its been mentioned in your Product model as attr_accessible.

    <% products.each do |product|%>
    
    
          <%= button_to "expired",
                        method_path(@path),
                        method: "get",   
                        class: product.expired ? "btn-hide-class" : "btn your-class-for-this-button",
                        remote: true
          %>
        </div>
      </div>
    <% end %>
    

    Hope, it will work for you :)