对布尔按钮使用助手 class
Using helper class for boolean button
我的按钮如下
<% 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%>
我需要为上面的视图代码提供助手 class,而不是重复按钮两次。谁能帮我一下
或者您可以只提供 button_to
帮助程序中的逻辑:
<%= button_to (user.active ? "Block" : "Unblock"), user_path(id: user.id, active: !user.active), class: 'btn btn-outline-dark', method: :patch %>
因此,如果您仍然认为它冗长,您可以将 (user.active ? "Block" : "Unblock")
逻辑移动到装饰器的助手中。
在助手中:
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
在模板中
<%= block_button(user.active) %>
我的按钮如下
<% 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%>
我需要为上面的视图代码提供助手 class,而不是重复按钮两次。谁能帮我一下
或者您可以只提供 button_to
帮助程序中的逻辑:
<%= button_to (user.active ? "Block" : "Unblock"), user_path(id: user.id, active: !user.active), class: 'btn btn-outline-dark', method: :patch %>
因此,如果您仍然认为它冗长,您可以将 (user.active ? "Block" : "Unblock")
逻辑移动到装饰器的助手中。
在助手中:
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
在模板中
<%= block_button(user.active) %>