Created
May 22, 2011 13:23
-
-
Save hiroshi/985457 to your computer and use it in GitHub Desktop.
content_for in controller as described in http://blog.yakitara.com/2011/05/rails-31-http-streaming-and-contentfor.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
... | |
# FORCE to implement content_for in controller | |
def view_context | |
super.tap do |view| | |
(@_content_for || {}).each do |name,content| | |
view.content_for name, content | |
end | |
end | |
end | |
def content_for(name, content) # no blocks allowed yet | |
@_content_for ||= {} | |
if @_content_for[name].respond_to?(:<<) | |
@_content_for[name] << content | |
else | |
@_content_for[name] = content | |
end | |
end | |
def content_for?(name) | |
@_content_for[name].present? | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
def index | |
content_for :title, "List of posts" | |
... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry to stir up an old topic, but what about memoizing the view_context? Will that cause other issues? Seems to work for me in rails 4.
EDIT
Found out real quick: Don't do this, it breaks instance variable assignment...