【问题标题】:method attributes [ajax,jquery,rails4]方法属性 [ajax,jquery,rails4]
【发布时间】:2014-01-24 10:10:59
【问题描述】:

我正在阅读《Agile web development with rails 4》一书。 有一部分产品的购物车仅在不为空时才显示,我的问题是视图中的函数仅向助手发送 2 个属性,而在实现中则有 3 个参数。

在视图中我有下面的代码,它呈现到 _cart 我有购物车的地方

<%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
        <%= render @cart %>
<% end %>

助手有:

module ApplicationHelper
def hidden_div_if(condition, attributes = {}, &block)
 if condition
  attributes["style"] = "display: none"
 end
 content_tag("div", attributes, &block) end
end

我的问题是 &block 在这种情况下接收 id: 'cart' 但它是可选的属性吗?这就是为什么它带有&。但是属性 = {} 呢? 我真的不确定这是怎么回事,有人可以解释一下吗?

谢谢!!

【问题讨论】:

    标签: ruby-on-rails ajax jquery ruby-on-rails-4


    【解决方案1】:

    doend 之间的代码是块,这是hidden_div_if 的第三个参数,它被简单地传递给content_taghidden_div_if 定义中的 &amp; 捕获您视图中的块,而对 content_tag 的调用中的 &amp; 再次扩展它以传递它。

    answer here 用几个例子很好地解释了这个想法。我建议您自己在irb 中测试所有内容以感受一下。

    【讨论】:

    • 很好,我会尝试做一些测试,让你知道!
    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 2011-02-13
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多