Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-2yield

Yield Rails 2 vs Rails 3


I was in process of migrating a rails 2 app to rails 3. While doing this i came accross a wiered behaviour with yield. I have a code snippet where i get the return value of yield.

x= true if yield 'xyz'

So if content_for exists for 'xyz' the value of x will be set true else nothing happens. It works fine in rails 2. But in rails 3, always true is set to x because rails returns an empty string even if 'xyz' is not available.

In Rails 2:

yield 'xyz' # nil if xyz is not available
yield 'xyz' # "hi" if xyz is available. "hi" is the rendered content

In Rails 3:

yield 'xyz' # "" if xyz is not available
yield 'xyz' # "hi" if xyz is available. "hi" is the rendered content

Why is this change? render_template has been changed completly?


Solution

  • I can not answer your questions but I'd recommend to use this approach:

    if content_for?(:xyz)
      yield(:xyz)
      x = true
    end