简体   繁体   中英

Ruby On Rails - Link_to

I have been working with ROR/bootstrap and want this "a" tag and images, to link back to root_path. But can't find a good solution. This is what I got. How to replace href?

<a class="navbar-brand" href="" style="color:black;">
    <!-- span makes them inline-->
    <span><%= image_tag ".png", height: 30, width: 30, alt: "", style:"margin-top:-5px" %></span>
    |
    <span><%= image_tag ".png", width: 60, height: 30, style:"margin-top:-5px"  %></span>
</a>

You could use one of the following approaches:

<a class="navbar-brand" href="<%= root_url %>" style="color:black;">
  <!-- span makes them inline-->
  <span>
    <%= image_tag ".png", height: 30, width: 30, alt: "", style:"margin-top:-5px" %>
  </span>
  |
  <span>
    <%= image_tag ".png", width: 60, height: 30, style:"margin-top:-5px"  %>
  </span>
</a>

or provide a block to link_to :

<%= link_to root_url, class: "navbar-brand", style: { color: "black" } do %>
  <!-- span makes them inline-->
  <span>
    <%= image_tag ".png", height: 30, width: 30, alt: "", style:"margin-top:-5px" %>
  </span>
  |
  <span>
    <%= image_tag ".png", width: 60, height: 30, style:"margin-top:-5px"  %>
  </span>
<% end %>

I'd strongly recommend going with the latter, as the Rails way of doing things. Let me know how you get on - any questions or comments, let me know!

You can use a block inside Link_to

<%= link_to root_url, :class => 'navbar-brand' do %>
  <span><%= image_tag ".png", height: 30, width: 30, alt: "", style:"margin-top:-5px" %></span>
  |
  <span><%= image_tag ".png", width: 60, height: 30, style:"margin-top:-5px"  %></span>
<% end %>

Avoid inline styling as you have currently used, push it into .css files.

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