In the Rails 3.2 CaptureHelper, what are the differences between using capture and content_for, and why would I choose one over the other?
In the Rails 3.2 CaptureHelper , what are the differences between using capture and
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you check out the content_for source, it calls capture internally:
So, from reading through the method, it looks like the primary advantage of content_for is that it can be called multiple times with multiple blocks for the same named content and each additional call will just append onto whatever has already been rendered. Whereas, in the case of capture, if you call:
and then later call:
Then the last part is the only part that will be captured, and the ‘Hello’ will just be discarded. Whereas, doing something similar in content_for will result in the second call being appended to ‘Hello’.