Skip to content

Instantly share code, notes, and snippets.

@zorn
Created August 30, 2011 20:36
Show Gist options
  • Save zorn/1181949 to your computer and use it in GitHub Desktop.
Save zorn/1181949 to your computer and use it in GitHub Desktop.
Trouble with nested content_tag in rails 3
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.
@lakelse
Copy link

lakelse commented Oct 11, 2011

Hi Zorn,

I had a similar problem earlier tonight -- hope this helps:

    content_tag('div', {:class => 'clearfix'}){ 
      form.label(symbol) +
        content_tag('div', {:class => 'input'}){ 
          form.send(input_type, symbol, html_options = {:class => 'xlarge'})
        }
    }

@zorn
Copy link
Author

zorn commented Oct 17, 2011 via email

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