【问题标题】:Rails link_to_if Class Doesnt Display CorrectlyRails link_to_if 类无法正确显示
【发布时间】:2013-08-02 02:43:34
【问题描述】:

对于我的生活,我无法弄清楚为什么会发生这个问题。我一直使用link_to helper,但iv只使用了几次link_to_if helper,这次我无法获取链接以获取CSS类。

这是我的链接

<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => @test_script), { :class => 'btn btn-mini' })%>

图像显示,没有预期的链接,但最后定义的 CSS 类没有,相反它只是没有类。这与我在所有 link_to 帮助程序中使用的格式相同。

谁能解释一下原因?

【问题讨论】:

    标签: ruby-on-rails link-to


    【解决方案1】:

    使用 link_to_if,如果条件失败,则仅返回名称 -- 在您的情况下为 raw('&lt;i class="icon-chevron-up"&gt;&lt;/i&gt;') 的结果--。否则将应用于链接标记的所有选项都将被忽略。

    请参阅source

    【讨论】:

      【解决方案2】:

      尝试传递一个空哈希作为选项参数,以便将{ :class =&gt; '...' } 分配给 html_options 参数。未经测试

      <%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => @test_script), {}, { :class => 'btn btn-mini' })%>
      

      【讨论】:

      • 谢谢,但没用。我得到:错误数量的参数(5 对 4)
      【解决方案3】:

      link_to_iflink_to_unless 包装在一个 span 中:

       %span.pull-right
         = link_to_unless Foo.deleted.empty?, "<i class='icon-white icon-trash'></i> Undelete Foo".html_safe, deleted_foos_path
      

      以上代码在显示的任何内容上设置了一个 css 类(向右拉) - 完整链接或仅其文本。

      【讨论】:

        猜你喜欢
        • 2018-01-24
        • 1970-01-01
        • 1970-01-01
        • 2014-04-23
        • 2018-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多