Ruby on rails 3 Rails 3.2使用content#u标记生成';删除';带有twitter引导图标的按钮

Ruby on rails 3 Rails 3.2使用content#u标记生成';删除';带有twitter引导图标的按钮,ruby-on-rails-3,twitter-bootstrap,link-to,content-tag,Ruby On Rails 3,Twitter Bootstrap,Link To,Content Tag,在本例中,我尝试使用Rails 3 content_标记方法在嵌套表单中复制Delete按钮图标,并以不引人注目的方式使用jQuery(至少我希望是这样) 使用Firebug检查时在中生成的html如下所示 <a class="btn btn-danger" href="#"> <i class="icon-trash icon-white"></i> Delete </a>

在本例中,我尝试使用Rails 3 content_标记方法在嵌套表单中复制Delete按钮图标,并以不引人注目的方式使用jQuery(至少我希望是这样)

使用Firebug检查时在中生成的html如下所示

<a class="btn btn-danger" href="#">
  <i class="icon-trash icon-white"></i>
  Delete
</a>

我使用以下命令生成带有图标的按钮,但无法向其添加“删除成分”,也无法获得href的“#”

以下是我在部分配料中的代码:

<%= link_to content_tag(:a, content_tag(:i, '', class: "icon-trash icon-white"), class: "btn btn-danger remove_fields") %>

这将产生:

<a class="btn btn-danger remove_fields">
  <i class=icon-trash icon-white"></i>
</a>

其中包含以下代码示例:

content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
# => <div class="strong"><p>Hello world!</p></div>
content\u标记(:div,content\u标记(:p,“helloworld!”),:class=>“strong”)
#=>你好,世界

有人能帮我指出正确的方向吗?为什么我遗漏了上面提到的细节? 注意:我可以使用link_to block来实现这个功能,但我想知道是否可以在一行中完成,而不使用do..end,更重要的是在content_for方法中

<%= link_to('#', class: "btn btn-danger remove_fields") do %>
  <i class: "icon-trash icon-white"></i>
  Delete
<% end %>

删除

为什么不创建一个视图助手方法呢

def link_to_delete
  link_to %{<i class="icon-trash icon-white"></i> Delete}, '#', class: "btn btn-danger remove_fields" 
end
我不太喜欢它的内容,但是:

content_tag(:span, %{#{content_tag(:i, nil, :class => "icon-trash icon-white")} Delete}.html_safe, :class => "btn btn-danger remove_fields")
如果您想要它带有a标记,只需将
:span
更改为
:a


<%= link_to(body, url, html_options = {}) %>
因此,您在1行中的请求是:

<%= link_to content_tag(:i, "", class: "icon-trash icon-white") + "Delete", path_to_stuff, method: :delete, class: "btn btn-danger remove_fields" %>

最复杂的部分是“身体”。您只需记住,所有这些内容标记帮助程序(包括链接到和其他)都返回一个字符串

但是,这是丑陋的。而且很长。而且很难维护。因此,你提出的解决方案,采取的块是更好的

<%= link_to content_tag(:i, "", class: "icon-trash icon-white") + "Delete", path_to_stuff, method: :delete, class: "btn btn-danger remove_fields" %>