Tooltip组件不适用于Simple Form和Bootstrap

时间:2014-09-17 21:44:24

标签: ruby-on-rails twitter-bootstrap ruby-on-rails-4 simple-form bootstrap-sass

我仔细地遵循了这些说明:https://github.com/plataformatec/simple_form/wiki/Bootstrap-component-helpers

但是我不断得到的问题是每当我的页面加载时,我的浏览器控制台中都会出现这个JS错误:

Uncaught TypeError: undefined is not a function

围绕这一行:

$('body').tooltip({ selector: "[data-toggle~='tooltip']"});

我认为导致问题的是.tooltip,因为初始化程序似乎正在运行。我可以告诉,因为生成的html具有JS选择器所需的data-toggle属性。

我正在使用bootstrap-sass gem。

这是我的simple_form_bootstrap初始化程序:

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

这是我的simple_form_components.rb初始化程序:

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)

我在_form.html.erb

中这样称呼它
<%= f.input :title, placeholder: "Enter Title", tooltip: ["bottom", "Must be as it appears in the BANK STATEMENT"] %>

这是我的post.js - 为简洁起见也被截断:

$(document).ready(function () {
    $('#count').click(counter);
    $('#post_body, #post_title').on('change keydown keypress keyup blur focus', counter);
        $('body').tooltip({ selector: "[data-toggle~='tooltip']"});
});

这是我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 .

1 个答案:

答案 0 :(得分:1)

您使用的simple_form初始值设定代码用于bootstrap2,对于您可能想要使用的bootstrap3:

# https://gist.github.com/mattclar/6315955#file-simple_form-rb
# http://stackoverflow.com/questions/14972253/simpleform-default-input-class
# https://github.com/plataformatec/simple_form/issues/316

inputs = %w[
  CollectionSelectInput
  DateTimeInput
  FileInput
  GroupedCollectionSelectInput
  NumericInput
  PasswordInput
  RangeInput
  StringInput
  TextInput
]
# DatepickerInput

inputs.each do |input_type|
  superclass = "SimpleForm::Inputs::#{input_type}".constantize

  new_class = Class.new(superclass) do
    def input_html_classes
      super.push('form-control')
    end
  end

  Object.const_set(input_type, new_class)
end

# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|

  config.boolean_style = :nested
  config.label_class = 'control-label'

  config.wrappers :overrides, tag: 'div', class: 'form-group', error_class: 'has-error',
                  defaults: { input_html: { class: 'default_class' } } do |b|

    b.use :html5
    b.use :min_max
    b.use :maxlength
    b.use :placeholder

    b.optional :pattern
    b.optional :readonly

    b.use :label_input
    b.use :hint,  wrap_with: { tag: 'span', class: 'help-block' }
    b.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' }
  end

  config.wrappers :prepend, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.wrapper tag: 'div', class: 'controls' do |input|
      input.wrapper tag: 'div', class: 'input-group' do |prepend|
        prepend.use :label , class: 'input-group-addon' ###Please note setting class here fro the label does not currently work (let me know if you know a workaround as this is the final hurdle)
        prepend.use :input
      end
      input.use :hint,  wrap_with: { tag: 'span', class: 'help-block' }
      input.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' }
    end
  end

  config.wrappers :append, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.wrapper tag: 'div', class: 'controls' do |input|
      input.wrapper tag: 'div', class: 'input-group' do |prepend|
        prepend.use :input
        prepend.use :label , class: 'input-group-addon' ###Please note setting class here fro the label does not currently work (let me know if you know a workaround as this is the final hurdle)
      end
      input.use :hint,  wrap_with: { tag: 'span', class: 'help-block' }
      input.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' }
    end
  end

  # Wrappers for forms and inputs using the Twitter Bootstrap toolkit.
  # Check the Bootstrap docs (http://getbootstrap.com/)
  # to learn about the different styles for forms and inputs,
  # buttons and other elements.
  config.default_wrapper = :overrides
  SimpleForm.browser_validations = false
end

这可能会让你更接近......