ruby-on-rails - simple_form collection_Radio_buttons 生成额外的标签

标签 ruby-on-rails ruby ruby-on-rails-4 simple-form simple-form-for

我有以下 ruby :

<%= simple_form_for @answer, url: presentation_survey_path(@presentation) do |f| %>
                <%= f.collection_radio_buttons :option_id, @question.options_array, :first, :last, {label: false} do |b| %>
    <div class="col-sm-4">
        <%= b.label(class: 'btn btn-large', style: 'margin-right: 20px; margin-left: 20px;') {b.radio_button(id: b.object.last, class: 'answer-input', style: 'display: none;') + b.text } %>
    </div>
<% end %>

它正确地生成了 html,除了它生成了两个标签:

<span>
    <label for="answer_option_id_15">
        <div class="col-sm-4">
            <label class="btn btn-large" for="answer_option_id_15" style="margin-right: 20px; margin-left: 20px;">
                <input class="answer-input" id="Way too many" name="answer[option_id]" style="display: none;" type="radio" value="15" />
            Way too many
            </label>
        </div>
    </label>
</span>

出于某种原因,它正在生成第一个标签。我只想保留第二个。标签:false 不起作用。如何去掉第一个标签?

最佳答案

根据docs :

For check boxes and radio buttons you can remove the label changing boolean_style from default value :nested to :inline.

此外,simple_form 有一些用于 collection_radio_buttons 的选项,这些选项记录在代码中:

# Collection radio accepts some extra options:
#
#   * checked  => the value that should be checked initially.
#
#   * disabled => the value or values that should be disabled. Accepts a single
#                 item or an array of items.
#
#   * collection_wrapper_tag   => the tag to wrap the entire collection.
#
#   * collection_wrapper_class => the CSS class to use for collection_wrapper_tag
#
#   * item_wrapper_tag         => the tag to wrap each item in the collection.
#
#   * item_wrapper_class       => the CSS class to use for item_wrapper_tag
#
#   * a block                  => to generate the label + radio or any other component.

在我的例子中,将 item_wrapper_tag 设置为 false 并将 boolean_style 设置为 :inline 就可以了,例如:

<%= f.collection_radio_buttons :option_id, @question.options_array, :first, :last, boolean_style: :inline, item_wrapper_tag: false do |b| %>

关于ruby-on-rails - simple_form collection_Radio_buttons 生成额外的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975087/

相关文章:

ruby-on-rails - 'secret_token' 环境 (Puma) 缺少 'secret_key_base' 和 'production'

ruby-on-rails - 如何在 Rails 中将日期格式化为 JSON?

ruby - 将元素添加到 postgres 数组字段失败,同时替换整个数组有效

ruby-on-rails - 你如何记录到 Cucumber 中的 Rails 日志文件?

ruby-on-rails - 为嵌套资源的 Show 和 Edit 操作指定正确的路径

ruby-on-rails - Elasticsearch on Query 的 Facet 问题

ruby-on-rails - 创建 100 个用户后为零用户

ruby - ruby 解释器如何判断您是在对整数调用方法还是在设置 float ?

没有 Rails 的 Ruby 的 `.present?`?

ruby-on-rails - Rails link_to 如果关联对象为 nil