【问题标题】:link_to_unless_current assign class to disabled (current) linklink_to_unless_current 将类分配给禁用(当前)链接
【发布时间】:2012-03-10 05:42:02
【问题描述】:

有人知道您是否可以为禁用或当前链接分配标签和类?下面的示例仅在当前链接的浏览器中显示为纯文本。

我有一些 Rails 代码显示数据库中每个设计的按钮列表。

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

活动链接被分配了正确的类并显示为按钮。

【问题讨论】:

    标签: html ruby-on-rails ruby css ruby-on-rails-3.1


    【解决方案1】:

    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
    %>
    

    在上面的示例中,如果当前页面是“新评论”页面,它将产生“返回”链接。

    【讨论】:

    • 谢谢,我确实在 API 网站上阅读了此信息。这个解决方案似乎有点手动,而不是我想要的。我希望仍然可以选择从 &lt;% @id_cards.each do |id| %&gt; 自动构建链接,任何其他提示将不胜感激。
    【解决方案2】:

    @James 给出了正确的答案,只是你太年轻了,不能正确回答 :)

    <% @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 %>
    

    【讨论】:

    • 感谢您澄清@James 的回复。您的示例效果很好。
    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2021-07-25
    • 1970-01-01
    • 2014-10-16
    相关资源
    最近更新 更多