2012-03-10 55 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