簡體   English   中英

Rails helper_method 可以像在相應視圖中調用它一樣使用 yield 嗎?

[英]Can a Rails helper_method use yield as if it was invoked in the corresponding view?

我有以下手風琴生成器,當直接包含在視圖中時可以正常工作:

<%
def collapser(name)
  fad = {
    class: 'collapsed',
    data: {toggle: 'collapse', parent: '#accordion_id'},
    href: "##{name}",
    aria: {expanded: 'true', controls: name}
  }
  tag.div(class: 'panel panel-default') do
    tag.div(class: 'panel-heading', role: 'tab') do
      tag.p(class: 'panel-title') do
        tag.a(fad) do
          tag.span do
            t("section.#{name}.title")
          end
        end
      end
    end +
    tag.div(id: name, class: 'panel-collapse collapse', role: 'tabpanel', style: 'height: 0px;', aria: {labelledby: name}, data: {parent: '#accordion_id'}) do
      tag.div(class: 'panel-body') do
        tag.div(class: 'uncode_text_column') do
          yield
        end
      end
    end
  end
end
%>

<%= tag.div(id: 'accordion_id', class: 'panel-group', role: 'tablist', aria: {multiselectable: 'true'}) do %>    
    <%= collapser('example') do %>
      <%= tag.p t('section.example.nub.row1') %>
    <% end %>
<% end %>

現在我想通過以下方式實現更干凈的實現:

  • collapser器移動到匹配的 controller
  • 制作一個generic_collapser(name, parent)所以
    • 在代碼庫的其他部分可以更廣泛地訪問它
    • 這個特定的折疊器可以通過調用generic_collapeser(name, 'accordion_id')來實現

但是我堅持第一步,因為我無法正確處理上下文更改。 首先, tag不再可訪問,但簡單地分配tag = view_context.tag似乎可以完成這項工作。 但是,我沒有找到轉置yield語句的方法。 我嘗試了以下

  • 保持tag.div(class: 'uncode_text_column') { yield }
  • 使用tag.div(class: 'uncode_text_column') { view_contex{yield} }
  • 使用tag.div(class: 'uncode_text_column') { view_contex(&block) }def collapser(name, &block)

但沒有人給出預期的結果。

也歡迎提供更好的資源以更好地理解view_contextyield和塊管理,尤其是帶有練習的教程。

Ruby 中的 &block 是什么? 它是如何在這里通過方法傳遞的?

因此,這里使用的關鍵特性是capture方法。 這是問題中指定的用於解決此問題的方法:

<% # in the view %>
<% content[:section_1] = capture do %>
  <%= tag.ul do %>
    <%= tag.li item 1 %>
    <%= tag.li item 2 %>
    <%= tag.li item 3 %>
  <% end %>
<% end %>

<% content[:section_2] = capture do %>
  <%= tag.p 'some paragraphe %>
<% end %>

<% bib = "accordion_#{SecureRandom.hex}" %>
<% title = ->(name){t("section.#{name}.title")} %>
<%= tag.div(id: bib, class: 'panel-group', role: 'tablist', aria: {multiselectable: 'true'}) do %>
  <% content.each do |key, nub| %>
    <%= collapser(key, title[key], nub, bib) %>
  <% end %>
<% end %>

而在另一邊

  # Within helpers
  # Returns a collapsable html div usable as an accordion item
  # Params:
  # +name+:: identifier for this div
  # +title+:: text used as title of the collapsable div
  # +content+:: text used as contont of the collapsable div
  # +parent+:: identifier of the parent accordion div
  def collapser(name, title, content, parent)
    link_fad = {
      class: 'collapsed',
      data: {toggle: 'collapse', parent: "##{parent}"},
      href: "##{name}",
      aria: {expanded: 'true', controls: name}
    }
    content_fad = {
      id: name,
      class: 'panel-collapse collapse',
      role: 'tabpanel',
      style: 'height: 0px;',
      aria: {labelledby: name},
      data: {parent: "##{parent}"},
    }
    tag.div(class: 'panel panel-default') do
      tag.div(class: 'panel-heading', role: 'tab') do
        tag.p(class: 'panel-title') do
          tag.a(link_fad) do
            tag.h2 do
              title
            end
          end
        end
      end + # note the plus here so we return a single string with the whole HTML
      tag.div(content_fad) do
        tag.div(class: 'panel-body') do
          tag.div(class: 'uncode_text_column' ) do
            content
          end
        end
      end
    end
  end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM