2012-03-10 72 views
1

任何人都知道您是否可以將標記和類分配給禁用或當前鏈接?下面的示例僅在瀏覽器中顯示爲當前鏈接的純文本。link_to_unless_current將類分配給禁用(當前)鏈接

我有一些rails代碼顯示數據庫中每個設計的按鈕列表。

<% @id_cards.each do |id| %> 
<%= link_to_unless_current id.design_type, id_card_design_path(id.id), :class => 'btn' %> 
<% end %> 

活動鏈接被分配正確的類並顯示爲按鈕。

回答

3

link_to_unless_current接受可用於覆蓋默認行爲的塊。

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_unless_current

<%= 
    link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do 
    link_to("Go back", { :controller => "posts", :action => "index" }) 
    end 
%> 

在上面的例子中,將產生「返回」鏈接,如果當前頁面是「新評論」頁面。

+0

謝謝你,我也讀了API網站上的這個信息。這個解決方案看起來有點手動,而不是我正在尋找的。我想仍然可以選擇從<%@ id_cards.each do | id |自動創建鏈接%>'任何額外的提示將不勝感激。 – prodigerati 2012-03-10 06:25:10

2

@詹姆斯給出正確的回答它只是你太年輕了,把它吧:)

<% @id_cards.each do |id| %> 
<%= 
link_to_unless_current(id.design_type, id_card_design_path(id.id), :class => 'btn') do 
    content_tag(:p, id.design_type, :class => :some_class 
end 
%> 
<% end %> 
+0

謝謝澄清@詹姆斯回覆。你的例子很好。 – prodigerati 2012-03-10 14:43:04

相關問題