简体   繁体   中英

link_to block not working

I'm using a series of link_to blocks to create buttons in my application. 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. 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:

<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:

<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 -

Generates a form containing a single button that submits to the URL created by the set of options.

So instead of your link_to , you'd be able to use:

<%= 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. You'll be much better styling the <a> tag itself, or not at all.

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 . If they're anything like ionIcons , they'll work by prepending the glyph with a :before pseudo class.

If this is the case, you don't need a separate <span> element to encapsulate them; just add the class to your link/button and the :before should be prepended.

Of course , if you want to style the span inside the link, you'll have to use the link_to block; however you need ensure you encase your text in the span instead of having it with no content.

Turned out I had dropped a class into my div that prevented the link from rendering ( add_fields was the class). Profoundly unsatisfying answer it is what it is. Thanks to anyone who took the time to give me a hint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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