I followed these instructions carefully: https://github.com/plataformatec/simple_form/wiki/Bootstrap-component-helpers
But the issue I keep getting is whenever my page loads, I get this JS error in my browser console:
Uncaught TypeError: undefined is not a function
Around this line:
$('body').tooltip({ selector: "[data-toggle~='tooltip']"});
I believe it is the .tooltip
that is causing the issue, because the initializer seem to be working. I can tell because the generated html has the data-toggle
attributes that are required for the JS selector.
I am using the bootstrap-sass
gem.
Here is my simple_form_bootstrap
initializer:
SimpleForm.setup do |config|
config.wrappers :bootstrap, tag: 'div', class: 'control-group', error_class: 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.use :tooltip
b.wrapper tag: 'div', class: 'controls' do |ba|
ba.use :input
ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
# Truncated for brevity
config.default_wrapper = :bootstrap
end
Here is my simple_form_components.rb
initializer:
module SimpleForm
module Components
module Tooltips
def tooltip
unless tooltip_text.nil?
input_html_options[:rel] ||= 'tooltip'
input_html_options['data-toggle'] ||= 'tooltip'
input_html_options['data-placement'] ||= tooltip_position
input_html_options['data-trigger'] ||= 'focus'
input_html_options['data-original-title'] ||= tooltip_text
nil
end
end
def tooltip_text
tooltip = options[:tooltip]
tooltip.is_a?(String) ? tooltip : tooltip.is_a?(Array) ? tooltip[1] : nil
end
def tooltip_position
tooltip = options[:tooltip]
tooltip.is_a?(Array) ? tooltip[0] : "right"
end
end
end
end
SimpleForm::Inputs::Base.send(:include, SimpleForm::Components::Tooltips)
I called it like this in my _form.html.erb
:
<%= f.input :title, placeholder: "Enter Title", tooltip: ["bottom", "Must be as it appears in the BANK STATEMENT"] %>
This is my post.js
- also truncated for brevity:
$(document).ready(function () {
$('#count').click(counter);
$('#post_body, #post_title').on('change keydown keypress keyup blur focus', counter);
$('body').tooltip({ selector: "[data-toggle~='tooltip']"});
});
This is the top part of my application.js
:
//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require bootstrap
//= require bootstrap/dropdown
//= require bootstrap/modal
//= require bootstrap/tooltip
//= require turbolinks
//= require_tree .