简体   繁体   English

link_to块不起作用

[英]link_to block not working

I'm using a series of link_to blocks to create buttons in my application. 我正在使用一系列link_to块在我的应用程序中创建按钮。 However I'm finding that these links don't end up working. 但是,我发现这些链接最终无法正常工作。 As I mouse-over the button, it recognizes the link, the correct url displays in the lower left-hand corner in firefox, but when I click, nothing happens. 当我将鼠标悬停在按钮上时,它可以识别链接,正确的URL显示在firefox的左下角,但是当我单击时,什么也没有发生。 Nothing comes up in my terminal or development log either. 我的终端或开发日志中也没有任何内容。

My code is as follows: 我的代码如下:

<%= link_to new_folder_path do%>
    <div class="btn btn-default add-button add_fields"><span class="glyphicon glyphicon-plus"></span>Add Folder</div>
<% end %>

This renders the following html: 这将呈现以下html:

<li>
  <a href="/folders/new">
    <div class="btn btn-default add-button add_fields"><span class="glyphicon glyphicon-plus"></span>Add Folder</div>
  </a>
</li>

I should also note that if I just type the standard link in without the do block, it runs just fine: 我还应该注意,如果我只是在不带do块的情况下键入标准链接,则它运行得很好:

<li><%= link_to "test", new_folder_path %></li>

Any thoughts on this would be much appreciated! 任何对此的想法将不胜感激!

尝试这个。

= link_to "<span class='glyphicon glyphicon-plus'></span>Add Folder".html_safe, new_folder_path, class: 'btn btn-default add-button add_fields'

You're probably better using button_to for the buttons - 您最好将button_to用于按钮-

Generates a form containing a single button that submits to the URL created by the set of options. 生成包含一个按钮的表单,该按钮提交到由选项集创建的URL。

So instead of your link_to , you'd be able to use: 因此,而不是你的link_to ,你可以使用:

<%= button_to "Add Folder", new_folder_path, class: "btn btn-default add-button add_fields glyphicon glyphicon-plus" %>

As a sidenote, if you're trying to style elements inside an <a> tag, you're going to have problems. 附带说明一下,如果您尝试 <a>标记内设置元素的样式,则会遇到问题。 You'll be much better styling the <a> tag itself, or not at all. 更好地设置<a>标记本身的样式,或者根本不样式。

As such, the issue you're getting could probably be resolved using the following: 因此,使用以下方法可以解决您遇到的问题:

<%= link_to "Add Folder", new_folder_path, class: "btn btn-default add-button add_fields glyphicon glyphicon-plus" %>

-- -

Icons 图标

I know Bootstrap includes Glyphicons . 我知道Bootstrap包括Glyphicons If they're anything like ionIcons , they'll work by prepending the glyph with a :before pseudo class. 如果它们像ionIcons类的ionIcons ,它们将在字形前面加上一个:before伪类来工作。

If this is the case, you don't need a separate <span> element to encapsulate them; 如果是这种情况, 不需要单独的<span>元素来封装它们; just add the class to your link/button and the :before should be prepended. 只需将类添加到您的链接/按钮中,然后:before

Of course , if you want to style the span inside the link, you'll have to use the link_to block; 当然 ,如果要在链接内部设置跨度样式,则必须使用link_to块; however you need ensure you encase your text in the span instead of having it with no content. 但是,您需要确保将文本括在span而不是不包含任何内容。

Turned out I had dropped a class into my div that prevented the link from rendering ( add_fields was the class). 原来我将一个类放到了div中,阻止了链接的呈现( add_fields是该类)。 Profoundly unsatisfying answer it is what it is. 深刻的不满意的答案是它是什么。 Thanks to anyone who took the time to give me a hint. 感谢所有花时间给我提示的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM