Search code examples
ruby-on-railsrubybuttonbooleanhelper

Using helper class for boolean button


My button is as follows

<% if user.active == true  %>
        <%= button_to "Block", user_path(id: user.id, active: false), class: 'btn btn-outline-dark',  method:  :patch %>
        <%else%>
        <%= button_to "Unblock", user_path(id: user.id, active: true), class: 'btn btn-outline-dark',  method: :patch %>
        <%end%>

I need to have helper class for the above view code instead of repeating the button twice. Can anyone help me with it


Solution

  • Or you can just provide the logic inside button_to helper:

    <%= button_to (user.active ? "Block" : "Unblock"), user_path(id: user.id, active: !user.active), class: 'btn btn-outline-dark',  method:  :patch %>
    

    Thus, if you still think it's verbose, you can move (user.active ? "Block" : "Unblock") logic into a helper of decorator.