Search code examples
ruby-on-railsruby-on-rails-5formbuilder

Joining Blocks of content_tag (Form Builder)


I am getting an number of errors and not sure how to join blocks of content_tags.

bs_form_builder.rb:36: syntax error, unexpected tSYMBEG, expecting end @template.content_tag :div, class: "input-group" do

bs_form_builder.rb:36: syntax error, unexpected do, expecting end ...g :div, class: "input-group" do ... ^~

bs_form_builder.rb:39: syntax error, unexpected '+', expecting end end + ^

bs_form_builder.rb:43: syntax error, unexpected end, expecting end-of-input end ^~~

class BsFormBuilder < ActionView::Helpers::FormBuilder

def bs_dollar_input(method, label)
    @template.content_tag :div, class: "form-group" do
      @template.label(@object_name, label)+
      @template.content_tag :div, class: "input-group" do
        @template.content_tag :div, class: "input-group-prepend" do
          @template.content_tag(:span, "$", class:'input-group-text')
        end +
        @template.text_field(@object_name, method, class: 'form-control')
      end
    end 
  end
 end

Anticipated result:

  <div class="form-group">
      <label>What is the value of your favorite car?</label>
      <div class="input-group">
          <div class="input-group-prepend">
              <span class="input-group-text">$</span>
          </div>
          <input class="form-control" id="car" name="car" required="" type="text" value="0" />
      </div>
  </div>

Solution

  • Needed to add () on the content_tags.

    def bs_dollar_input(method, label)
        @template.content_tag :div, class: "form-group" do
          @template.label(@object_name, label)+
          @template.content_tag(:div, class: "input-group") do
            @template.content_tag( :div, class: "input-group-prepend") do
              @template.content_tag(:span, "$", class:'input-group-text')
            end +
            @template.text_field(@object_name, method, class: 'form-control')
          end
        end 
      end
     end