Ruby 将链接或按钮作为组件传递给SimpleForm包装器

Ruby 将链接或按钮作为组件传递给SimpleForm包装器,ruby,forms,ruby-on-rails-4,simple-form,Ruby,Forms,Ruby On Rails 4,Simple Form,我希望输入像以下模板一样呈现: <div class="form-group"> <div class="input-group"> <input class="form-control" type="text"> <span class="input-group-btn"> <button type="button" class="btn btn-primary" id="inline-button"&g

我希望输入像以下模板一样呈现:

<div class="form-group">
  <div class="input-group">
    <input class="form-control" type="text"> 
    <span class="input-group-btn"> 
      <button type="button" class="btn btn-primary" id="inline-button">Do Action!</button> 
      <!-- this could be a link or a button -->
    </span>
  </div>
</div>

解决了。输入的工作原理如下:

<%= form.input :some_button, wrapper: "inline", inline_button:  link_to("Something", "#") %>
然后,在config/initializers/simple_form/inline_button_component.rb中

module SimpleForm
  module Components
    # Needs to be enabled in order to do automatic lookups
    module InlineElements
      # Name of the component method
      def inline_element(wrapper_options = nil)
        @inline_element ||= begin
          options[:inline_element].to_s.html_safe if options[:inline_element].present?
        end
      end

      # Used when the number is optional
      def has_inline_element?
        inline_element.present?
      end
    end
  end
end

SimpleForm::Inputs::Base.send(:include, SimpleForm::Components::InlineElements)
<%= form.input :some_button, wrapper: "inline", inline_button:  link_to("Something", "#") %>
config.wrappers "inline_button", tag: 'div', class: 'form-group' do |ib|
...
  ib.wrapper tag: 'div', class: 'input-group col-lg-10' do |ig| 
    ig.use :input, class: 'form-control'
    ig.wrapper tag: 'span', class: 'input-group-btn' do |btn|
      btn.use :button, class: 'btn btn-primary
    end
  end
end
module SimpleForm
  module Components
    # Needs to be enabled in order to do automatic lookups
    module InlineElements
      # Name of the component method
      def inline_element(wrapper_options = nil)
        @inline_element ||= begin
          options[:inline_element].to_s.html_safe if options[:inline_element].present?
        end
      end

      # Used when the number is optional
      def has_inline_element?
        inline_element.present?
      end
    end
  end
end

SimpleForm::Inputs::Base.send(:include, SimpleForm::Components::InlineElements)