Created
August 30, 2011 20:36
-
-
Save zorn/1181949 to your computer and use it in GitHub Desktop.
Trouble with nested content_tag in rails 3
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
In my app I made a helper method to help me render form rows and DRY up my code. | |
def form_row(form, input_type, symbol) | |
# Need to generate HTML like this | |
# <div class="clearfix"> | |
# <%= f.label :first_name %> | |
# <div class="input"> | |
# <%= f.text_field :first_name, html_options = {:class => 'xlarge'} %> | |
# </div> | |
# </div><!-- /clearfix --> | |
# I tried to write the method like below but never got it to work right. | |
# got the sense that content_tag only enclosed the final line in its enclosure. | |
# The below code for example runs but the form.label(symbol) isn't included in the output. | |
# I tried adding a '+' ala the SO suggestion but it only gave me syntax errors. | |
# Any suggestions? | |
# Found these questions but didn't find a working answer (or maybe I'm misunderstanding something) | |
# http://stackoverflow.com/questions/4205613/rails-nested-content-tag | |
# http://robots.thoughtbot.com/post/9123352587/nesting-content-tag-in-rails-3 | |
output = content_tag 'div', {:class => 'clearfix'} do | |
form.label(symbol) # doesn't render | |
content_tag 'div', {:class => 'input'} do | |
form.send(input_type, symbol, html_options = {:class => 'xlarge'}) | |
end | |
end | |
return output | |
end | |
Thanks for your suggestions. |
Thanks for the email. I'll give this a shot.
Jonathan MacDonald wrote:
Hi Zorn,
I had a similar problem earlier tonight -- hope this helps:
``` ruby
content_tag('div', {:class => 'clearfix'}){
form.label(symbol) +
content_tag('div', {:class => 'input'}){
form.send(input_type, symbol, html_options = {:class => 'xlarge'})
}
}
```
## ~ Mike
Michael Zornek
Clickable Bliss
http://clickablebliss.com/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Zorn,
I had a similar problem earlier tonight -- hope this helps: