Rails: complex html <a> in link_to helper

How do I convert this link if I wanted to use the rails <%= link_to %> helper?

Here is my link:

 <a id="landing_link" href="#landing"><div id="headerlogo"></div></a> 

Another question is that a link is an anchor link. In link_to from another page, how would I link it to a page (like an index) and then to an anchor?

+4
source share
4 answers

You can use the do block for link_to:

Documentation their example:

 <%= link_to(@profile) do %> <strong><%= @profile.name %></strong> -- <span>Check it out!</span> <% end %> # produces the following HTML: <a href="/profiles/1"> <strong>David</strong> -- <span>Check it out!</span> </a> 

In your case , <a id="landing_link" href="#landing"><div id="headerlogo"></div></a> It becomes :

 <%= link_to '#landing', :id => 'landing_link' do %> <div id="headerlogo"></div> <% end %> 
+4
source
 <%= link_to "", "#landing", :id => 'landing_link' do %> <div id="headerlogo"></div> <% end %> 
+2
source

You can pass the block to the link_to helper method: (HAML)

 = link_to("#landing", :id => "landing_link") do .headerlogo 
0
source

Something like that

 <%= link_to content_tag(:div, '', :id=>'headerlogo'), '#landing', :id=>'landing_link' %> 
0
source

Source: https://habr.com/ru/post/1447344/


All Articles