【问题标题】:How do I get rid of the div that wraps every input in a simple_form?如何摆脱将每个输入包装在 simple_form 中的 div?
【发布时间】:2013-02-09 03:37:03
【问题描述】:

这个输入:

<%= f.input :keywords, label: false, :input_html => {:class => "span8"}, :placeholder => "Park Slope, Prospect Heights, Fort Green..." %>

产生这个:

<div class="control-group string optional">
  <div class="controls">
     <input class="string optional span8" id="search_keywords" name="search[keywords]" placeholder="Park Slope, Prospect Heights, Fort Green..." size="50" type="text" />
  </div>
</div>

如何在没有 div 的情况下单独生成 input

谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 simple-form


    【解决方案1】:

    像这样传递参数wrapper: false 像这样

    <%= f.input :keywords, 
                label: false,
                wrapper: false,
                input_html: { class: 'span8' }, 
                placeholder: 'Park Slope, Prospect Heights, Fort Green...' %>
    

    并且看到它会起作用

    希望有帮助

    【讨论】:

    • 快到了。它摆脱了包装器 div 之一。即从问题中,它用class="control-group string optional" 摆脱了div。它并没有摆脱div class=controls。所以布局仍然被打破。想法?
    • 唯一的包装代码在问题中。你可以看到input 周围的两个div。一个类为control-group string optional,另一个类为controls。此外,f.input...simple_form 助手会生成该输出。你还想看其他东西吗?
    • 哦...这是一个非常受欢迎的宝石 - 你可以在这里看到源代码 - github.com/plataformatec/simple_form
    • @marcamillion 对不起,我没有要求 gem 源我要求 simple_form gem 为你生成的文件,使用它在你的初始化文件夹中生成我的意思是 config/intializer/simple_form.rb
    【解决方案2】:

    看来最好的方法是使用f.input_field

    Simple_Form 的文档并没有完全说明,但您可以查看实际的 API docs here

    来自文档:

    simple_form_for @user do |f|
      f.input_field :name
    end
    

    将产生:

    <input class="string required" id="user_name" maxlength="100"
       name="user[name]" size="100" type="text" value="Carlos" />
    

    【讨论】:

      【解决方案3】:

      使用普通的text_field

      <%= f.text_field :keywords, :class => "string optional span8", :placeholder => "Park Slope, Prospect Heights, Fort Green..." %>
      

      【讨论】:

      • 今晚你很兴奋,你不在家吗。好想法!但问题是……我仍然希望将simple_form_for 字段用于我表单中的其他一些元素。有没有办法禁用那些包装器 div?
      猜你喜欢
      • 2010-11-13
      • 2021-06-03
      • 2011-08-07
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 2020-02-26
      • 2011-03-22
      • 2019-07-27
      相关资源
      最近更新 更多