Recently, I ran into a situation where I wanted to have controller specific information in the layout. The easiest solution I came up with was to use the render_to_string method to set the content in a filter, like so:
before_filter :set_infobar_content
protected
def set_infobar_content
@infobar_content = render_to_string :partial => 'infobar_content'
end
It was extra sweet because I just had to create a _infobar_content.html.erb view for each controller, and they’d all (magically) get their infobar content. Everything was fine until I ran my tests. That’s when I got this:
expecting <"index"> but rendering with <"_infobar_content">
Uh oh! I finally the solution in a ticket for Rails.
response.template.first_render = nil
Now my method looks like this:
def set_infobar_content
@infobar_content = render_to_string :partial => 'infobar_content'
# The render_to_string method above screws up the assert_template methods
# in the tests, so this is how we 'reset' it.
response.template.first_render = nil
end
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.