【问题标题】:Passing a link or a button as components to SimpleForm wrappers将链接或按钮作为组件传递给 SimpleForm 包装器
【发布时间】:2016-03-22 13:49:22
【问题描述】:

我希望像这个模板一样渲染输入:

<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>

我想要一种通过 SimpleForm 创建表单的方法,将生成的按钮传递给简单的表单包装器,以便将其用作包装器中的组件:

<%= simple_form_for(something) do |form| %>
  <%= form.input :some_attribute, wrapper: "inline_button", button: "inline-button" %>
  <%= button_tag "inline-button" %>
<% end %>

(我知道这段代码行不通,这只是一个例子)

包装器是:

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

【问题讨论】:

    标签: ruby forms ruby-on-rails-4 simple-form


    【解决方案1】:

    解决了。输入是这样的:

    <%= 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
    

    然后,在 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)
    

    【讨论】:

      猜你喜欢
      • 2011-06-02
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 2017-05-09
      • 2017-06-25
      • 2012-12-04
      相关资源
      最近更新 更多