如果我想使用rails的幫助文件<%= link_to %>
helper,我該如何轉換此鏈接?Rails:複雜html <a>轉換爲link_to幫手
這裏是我的鏈接:
<a id="landing_link" href="#landing"><div id="headerlogo"></div></a>
另一個問題是,該鏈接是一個錨鏈接。在另一個頁面的link_to中,我將如何將它鏈接到頁面(例如索引),然後鏈接到錨點?
如果我想使用rails的幫助文件<%= link_to %>
helper,我該如何轉換此鏈接?Rails:複雜html <a>轉換爲link_to幫手
這裏是我的鏈接:
<a id="landing_link" href="#landing"><div id="headerlogo"></div></a>
另一個問題是,該鏈接是一個錨鏈接。在另一個頁面的link_to中,我將如何將它鏈接到頁面(例如索引),然後鏈接到錨點?
可以使用do
塊的link_to:
The documentation;他們的例子:
<%= 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>
在你的情況,<a id="landing_link" href="#landing"><div id="headerlogo"></div></a>
變爲:
<%= link_to '#landing', :id => 'landing_link' do %>
<div id="headerlogo"></div>
<% end %>
<%= link_to "", "#landing", :id => 'landing_link' do %>
<div id="headerlogo"></div>
<% end %>
你可以傳遞一個塊到的link_to helper方法: (HAML)
= link_to("#landing", :id => "landing_link") do
.headerlogo
Somethi ng像這樣
<%= link_to content_tag(:div, '', :id=>'headerlogo'), '#landing', :id=>'landing_link' %>
我注意到,你在使用id來選擇你的視圖中的DOM元素。在最好的Rails實踐中聲明,在視圖(樣式表)中DOM元素是按類和.js(.coffee) - 按ID選擇的。 –