Search code examples
ruby-on-railstwitter-bootstrapbootstrap-datetimepickercontent-tag

bootstrap datepicker with rails content_tag


How do you write the following code using the rails content_tag helper:

<div class="input-group date" data-provide="datepicker">
    <input type="text" class="form-control">
    <div class="input-group-addon">
        <span class="glyphicon glyphicon-th"></span>
    </div>
</div>

Solution

  • You can write this code with content_tag following bellow steps -

    Step 1. Go to your application helper files and add below helper method inside application helper file -

    # app/views/helpers/application_helper.rb
    
    def generate_date_picker_field 
      content_tag :div, class: "input-group date", :data => {:provide => 'datepicker'} do
      concat text_field_tag nil, nil, class: 'form-control', id: nil
      concat content_tag :div, class: "input-group-addon", &-> do
        content_tag :span, nil, class: 'glyphicon glyphicon-th'
      end
    end
    

    end

    Step - 2. Call this helper method to your view files. For example -

    # app/views/layouts/application.html.erb
    <body>
     <%= yield %>
     <%= generate_date_picker_field %>
    </body>
    

    Hope it should work.