Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content_for is not working as described when content is not set #9360

Closed
killthekitten opened this issue Feb 21, 2013 · 9 comments
Closed

content_for is not working as described when content is not set #9360

killthekitten opened this issue Feb 21, 2013 · 9 comments

Comments

@killthekitten
Copy link
Contributor

Following this example from documentation, content_for can be used as a part of boolean statement:

...
# <tt>content_for</tt>, however, can also be used in helper modules.
#
#   module StorageHelper
#     def stored_content
#       content_for(:storage) || "Your storage is empty"
#     end
#   end
...

But it does not behave as described. content_for output is wrapped with ActiveSupport::SafeBuffer which casts nil value to an empty string instead of just returning nil.

To be clear, I expect content_for to return nil if no content is set. Here is my case:

# If content for header was passed, render it, else render default partial 
= content_for(:header) || render 'partials/header'

And it does not work as expected. My current workaround:

= content_for?(:header) ? content_for(:header) : render 'partials/header'

Do we need to update example? Or is it a bug?

@sikachu
Copy link
Member

sikachu commented Feb 21, 2013

If that's define in the doc like that, I think that's a bug/regression. Good finding!

Would you mind submitting a failing test case, or even better a fix for this? Thank!

@senny
Copy link
Member

senny commented Feb 21, 2013

I would also trust the documentation if this was added to the docs it once was a use-case.

@killthekitten I could take a look and submit a PR to solve the problem. Let me know if you are not working on it.

@killthekitten
Copy link
Contributor Author

@sikachu sure, I'll do it. But currently I have no idea how to make actual fix. @senny any thoughts?

@senny
Copy link
Member

senny commented Feb 21, 2013

@killthekitten I did not look into the source yet... but the problem seems straight forward. You could look what commit added the example in the documentation and take a look at how the code looked back then to ensure if it worked once. Then write a failing test-case and as last step change the code so that it passes your newly written test and all others on AP/AV.

@sikachu
Copy link
Member

sikachu commented Feb 21, 2013

Yeah, I'm pretty sure it was overlooked when we introduced SafeBuffer.

@killthekitten
Copy link
Contributor Author

Sad, this PR breaks many tests. Didn't run the full suite before commit.

@killthekitten
Copy link
Contributor Author

This commit is better, but I don't like the code complication that it brings. What do you think? /cc @sikachu @senny

rafaelfranca added a commit that referenced this issue Feb 28, 2013
@sadfuzzy
Copy link
Contributor

sadfuzzy commented Nov 5, 2014

Hey, it is broken again in 3.2.19 :) and defaults to an empty string

@killthekitten
Copy link
Contributor Author

@sadfuzzy and did you check if tests are also failing (probably they don't, but can you provide us with a failing case?)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants