Forked from bunnymatic/nested_content_snippet.rb
Last active
October 28, 2023 21:06
-
-
Save abhilashak/a67eab371c54e1e71e1002f9c2dd791d to your computer and use it in GitHub Desktop.
Nested content tags in rails 5 view helpers
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
# because i can never remember exactly how and when to use concat | |
# when building content in helpers | |
def nested_content | |
content_tag 'div' do | |
concat(content_tag 'span', 'span block') | |
concat(tag 'br') | |
concat(link_to 'root link', root_path) | |
concat(tag 'br') | |
concat(link_to('#') do | |
concat(content_tag 'h2', 'Head \'em off') | |
concat(tag 'br') | |
content_tag 'div', (content_tag 'span',(concat 'dudue')) | |
end) | |
end | |
end | |
Another Example for Bootstrap 4 with specifying mandatory field etc. | |
def form_field_tag(type, label, attr, column, opts={}) | |
content_tag(:div, class: 'form-group row') do | |
concat( | |
label_tag("#{attr}[#{column}]", class: 'col-lg-3 col-form-label form-control-label') do | |
concat label | |
concat content_tag(:span, ' *', class: 'text-danger') if opts[:required] | |
end | |
) | |
concat( | |
content_tag(:div, class: 'col-lg-9') do | |
case type | |
when 'text_field' | |
concat text_field_tag("#{attr}[#{column}]", nil, | |
class: 'form-control', 'aria-describedby' => opts[:hint_id], | |
required: opts[:required]) | |
when 'text_area' | |
concat text_area_tag("#{attr}[#{column}]", nil, | |
class: 'form-control', 'aria-describedby' => opts[:hint_id], | |
required: opts[:required]) | |
when 'select' | |
concat select_tag("#{attr}[#{column}]", options_for_select(opts[:select_options], opts[:selected_option]), | |
class: 'form-control', 'aria-describedby' => opts[:hint_id], | |
required: opts[:required]) | |
end | |
concat content_tag(:small, opts[:hint], id: opts[:hint_id], | |
class: 'form-text text-muted') | |
end | |
) | |
end | |
end | |
= form_field_tag('text_field', 'Source', 'article', 'source', hint: "Ex: journel", hint_id: 'media_SourceHelp', required: true) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!!