Reputation: 109
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
Upvotes: 0
Views: 54
Reputation: 739
In helpers:
def block_button(is_active)
button_to is_active ? 'Block' : 'Unblock', user_path(id: user.id, active: !is_active), class: 'btn btn-outline-dark', method: :patch
end
In template
<%= block_button(user.active) %>
Upvotes: 0
Reputation: 15045
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.
Upvotes: 1