ruby-on-rails - SimpleForm 2 : input tags inside a wrapper

标签 ruby-on-rails ruby-on-rails-3

通过使用默认的simple_form 2包装器,它会生成一个如下所示的标记:

<div class="...">
  <label>...</label>
  <input ... />
</div>

我想获得一个标记,其中输入标签本身就是这样的包装器:
<div class="...">
  <label>...</label>
  <div class="...">
    <input ... />
  </div>
</div>

我是否必须为此行为创建一个自定义组件?

谢谢。

尼古拉斯

最佳答案

看一下这个:

包装器API的文档尚不完善,但我花了一些时间试图弄清楚。这是一个简单的示例,可以在simple_form初始化程序中进行设置。

# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
  # Wrappers are used by the form builder to generate a complete input.
  # You can remove any component from the wrapper, change the order or even
  # add your own to the stack. The options given to the wrappers method
  # are used to wrap the whole input (if any exists).

  config.wrappers :GIVE_YOUR_WRAPPER_A_NAME, :class => 'clearfix', :error_class => nil do |b|
    b.use :placeholder
    b.use :label
    b.use :tag => 'div', :class => 'WHATEVER_YOU_WANT_TO_CALL_IT' do |ba|
      ba.use :input
      ba.use :error, :tag => :span, :class => :'help-inline'
      ba.use :hint,  :tag => :span, :class => :'help-block'
    end
  end
end

然后,在创建表单时,只需指定要使用的包装器即可:
<%= simple_form_for @user, wrapper: 'WHATEVER_YOU_CALLED_YOUR_WRAPPER' do |form| %>
<%end%>

希望这可以帮助。

关于ruby-on-rails - SimpleForm 2 : input tags inside a wrapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487601/

相关文章:

javascript - Rails javascript Assets 根据路由表现异常

ruby-on-rails - 使用 capistrano 和 svn 部署 rails 时,您将 app-config-files 放在哪里

ruby-on-rails - rails : Why shouldn't I directly make changes directly in schema than do migrate

ruby-on-rails - 您使用哪个 Ruby on Rails 管理插件?为什么?不同管理 gem 的优缺点是什么?

ruby-on-rails - 如何用简单的形式定义 Action ?

ruby-on-rails-3 - 如何销毁resque worker 排队的工作?

ruby-on-rails - 渲染图像

ruby-on-rails - Authlogic OpenID 错误:未初始化的常量 OpenIdAuthentication::InvalidOpenId

ruby-on-rails - Capistrano 杀死 Assets :precompile

ruby-on-rails - rails : How to run `rails generate scaffold` when the model already exists?