【问题标题】:simple_form collection_Radio_buttons generates extra labelssimple_form collection_Radio_buttons 生成额外的标签
【发布时间】:2015-04-30 18:27:05
【问题描述】:

我有以下 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 不起作用。如何摆脱第一个标签?

【问题讨论】:

  • 根据文档,你做对了,但唯一的区别是你在两行之间注入了直接的 html。你能放下它,看看会发生什么吗?如果可行,请尝试使用 content_tag,或尝试在 js 或 css 中应用类和样式。

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


【解决方案1】:

根据docs

对于复选框和单选按钮,您可以删除标签更改 boolean_style 从默认值 :nested 到 :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 设置为falseboolean_style 设置为:inline 就可以了,例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-27
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多