我创建了以下自定义帮助程序来添加带图标的链接:
module ApplicationHelper
def link_with_icon_to(label, icon, url, data = {}, method = nil)
render "shared/link_with_icon", label: label, icon: icon, url: url, data: data, method: method
end
def edit_link(url)
link_with_icon_to 'Editar', 'fa fa-pencil', url
end
def remove_link(url)
link_with_icon_to 'Excluir', 'fa fa-trash', url, method: :delete, data: { confirm: 'Are you sure?' }
end
end
_link_with_icon.html.erb
<%= link_to url, method: method, data: data do %>
<i class="<%= icon %>" /><span><%= label.html_safe %></span>
<% end %>
我正在以这种方式使用它们:
<% @insurance_companies.each do |insurance_company| %>
<tr>
<td><%= insurance_company.name %></td>
<td><%= insurance_company.cnpj %></td>
<td><%= edit_link edit_insurance_company_path(insurance_company) %></td>
</td>
<td><%= remove_link insurance_company %></td>
</tr>
<% end %>
但是remove_link
帮助程序无效。不会显示确认信息,也不会删除数据。
我的助手有什么问题?
答案 0 :(得分:0)
这是可能的,因为您没有将其他选项(数据和方法)传递给link_to。
无法立即确认/验证这一点,但我认为您可以解决此问题的方法是:
def link_with_icon_to(label, icon, url, opts = {})
render "shared/link_with_icon", label: label, icon: icon, url: url, opts: opts
# there definitely better ways of handling this, but can't think of another right now
end
# _link_with_icon.html.erb
# spread the opts hash here. the opts hash could be {data: {element: 'whatever'}, method: :delete} etc.
<%= link_to url, **opts do %>
<i class="<%= icon %>" /><span><%= label.html_safe %></span>
<% end %>