no block given (yield) in partial [on hold]

问题内容:

Based on this answer, I have a partial located in foo/_bar.html.erb:

<div class="foo">
  <%= label %>: <%= capture($block) %>
</div>

I want to render it from the code, when calling another method, specifically a helper:

 def foo (label, &block)
   render 'foo/bar', locals: { label: label, block: block }
 end

I call the foo helper like this:

<%= foo 'Hello world' do %>
   <h1>Hiiii</h1>
<%= end %>

When running the code, I get this error:

no block given (yield)

(from the partial code)

If I remove the capture snippet, the label is rendered correctly.

How can I render the &block in the _bar partial?

问题评论:

1  
def foo (label, &block) should be def foo(label, &block) (not sure if that’s the problem, though)
1  
$ and & are two different symbols, capture($block) expects a global variable holding block.
    
@mudasobwa Ahh, typos! Thanks for spotting that! You can leave an answer. Maybe others will have the same issue in the future and will come here. :-)
– Ionică Bizău
5 hours ago
    
I doubt this particular case deserves it’s own answer 🙂
    
@IonicăBizău as you probably know, we close questions regarding problems that were caused by typos 😉

原文地址:

https://stackoverflow.com/questions/47750476/no-block-given-yield-in-partial

添加评论

友情链接:蝴蝶教程