为什么我要把病情放两次?

问题描述 投票:0回答:1

我真的没有问题,但我不明白解决方案。

在项目视图(显示)上,我检查查看器是否已连接。如果没有,我会根据优惠数量在某个位置呈现横幅。

提供视图(在项目视图中呈现):

- @offers.each_with_index do |offer, index|

   - if !user_signed_in? && (@project.published? || @project.pending_attribution?)
      - if cta_banner_position(@offers, index)
         = render 'projects/cta_banner_guest_presta.html.haml'

cta_banner_position助手:

def cta_banner_position(offer, index)
   index == 2 || (offer.size == 2 && index == 1) || (offer.size == 1 && index == 0)
end

渲染的横幅:

.row
   .col-md-12
      .project-cta.p-5.mb-2.text-center
         %h5
            %strong
               blabla
          %p.text-muted blabla
          %p.m-0
             = link_to "Send a quote", "", class: "btn btn-primary px-3", data: { toggle: 'modal', target: '#modal-sign' }, onclick: "ga('send', 'event', 'button', 'Clic', 'Projects signup inoffers');", tabindex: -1

我使用each_with_index获取位置并设置横幅,这是有效的,但是,如果我已连接,我可以看到横幅(尽管有!user_signed_in条件)仅在没有报价的情况下。

我不明白为什么在连接时我有渲染的横幅。要解决这个问题,我还必须在横幅文件的顶部添加“ - if!user_signed_in?”。当我这样做时,横幅停止显示我是否已登录。

任何人都可以向我解释为什么他会忽略第一个条件,特别是为什么只有在没有报价时呢?

ruby-on-rails ruby haml
1个回答
0
投票

你的助手什么都不做。如果满足条件(或者不符合条件),您只需返回nil。相反,只需返回那个长布尔逻辑并有一个- if cta_banner_position(@offers, index)。这将解决@ fl00r提到的缩进问题,因为以前没有任何东西可以嵌套你的cta横幅。

我不认为帮助器之前甚至被调用过,通常需要一个 - 在Ruby之前,它以haml执行

© www.soinside.com 2019 - 2024. All rights reserved.