【问题标题】:Rails - get toggle-button status in controllerRails - 在控制器中获取切换按钮状态
【发布时间】:2015-11-23 01:12:55
【问题描述】:

我做了很长时间的研究,但找不到解决方案。我想在我的表单中制作一个切换按钮列表,然后保存用户检查的那些。我知道我可以命名切换按钮,然后通过 params[:togglebutton-name] 在控制器中找到它,但似乎切换按钮没有任何价值。例如,我有 5 个切换按钮,用户将检查其中的两个。然后我想在Controller中了解它。我该怎么办?

切换按钮:

<% current_user.type_tags.each do |type_tag| %>
            <button name="type<%= type_tag.id %>" id="associated-type-tag-<%= type_tag.id %>" type="button" class="btn btn-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
                <%= type_tag.name %>
            </button>
        <% end %>

控制器:

def create
  @try = params[:type1]
  render 'help'
end

帮助:

Value: <%= @try %>;

结果我有这个:

Value: ;

【问题讨论】:

    标签: ruby-on-rails togglebutton


    【解决方案1】:

    我已经找到了解决方案。我使用 btn-group 和数据切换:“按钮”。在里面我有标签和输入类型=复选框。如果按下按钮,这种方法允许我在我的控制器中获得一个真实的值。我也可以自己设置按钮样式。

    我希望它对某人有用。

    <div class="btn-group" data-toggle="buttons">
                <% current_user.type_tags.each do |type_tag| %>
                    <label class="btn btn-primary">
                        <input name="type<%= type_tag.id %>" id="associated-type-tag-<%= type_tag.id %>" type="checkbox" autocomplete="off">
                            <%= type_tag.name %>
                        </input>
                    </label>
                <% end %>
            </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      相关资源
      最近更新 更多