自动选择表单中的值

时间:2017-09-26 14:29:46

标签: ruby-on-rails forms

我在我的产品表单上应用了一个模板,我知道如何在我的编辑表单中自动选择定义的值

<div class="btn-group" data-toggle="buttons">
        <label class="btn btn-danger" style="margin-right: 10px;">
          <input id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="0"/> Aucune
        </label>
        <label class="btn btn-danger" style="margin-right: 10px;">
          <input id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="1"/> 1
        </label>
        <label class="btn btn-danger" style="margin-right: 10px;">
          <input id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="2"/> 2
        </label>
        <label class="btn btn-danger" style="margin-right: 10px;">
          <input id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="3"/> 3
        </label>
        <label class="btn btn-danger" style="margin-right: 10px;">
          <input id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="4"/> 4
        </label>
        <label class="btn btn-danger" style="margin-right: 10px;">
          <input id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="5"/> 5 et +
        </label>
        </div>

如果我的产品价值为1,我希望1

上有一个活动按钮

1 个答案:

答案 0 :(得分:1)

您可以使用checked属性,从数据库/控制器中转出值,如下所示:

<input <%= "checked" if product == 1 %> id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="1"/>
<input <%= "checked" if product == 2 %> id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="2"/>
<input <%= "checked" if product == 3 %> id="bien_nb_piece_true" name="bien[nb_piece]" type="radio" autocomplete="off" value="3"/>

等等。