获取数据库值以使用Rails 3选择选项标记

时间:2015-04-23 11:28:08

标签: ruby ruby-on-rails-3.2

我想将db值设置为select选项标签,并使用Rails 3在下拉列表中显示它们。假设用户模型有5个字段,我需要用户模型的所有名称列值将在选择选项中设置到下拉列表中tag.When页面将被加载(ie-home.html.erb)所有名称字段值应从DB获取并在select标签中设置。请帮我解决此问题。

Home.html.erb:

<div class="block-title"><h5>Office Use</h5></div>
            <div class="block-content full">
                    <div class="totalaligndiv">
                      <div class="input-group bmargindiv1 col-md-6" style="margin:auto; float:none;"><span class="input-group-addon text-left"><div class="leftsidetextwidth">Select Vendor :</div></span>
                        <select class="form-control">
                          <option selected>Selected Vendor</option>
                          <option>1</option>
                            <option>2</option>
                            <option>3</option>
                        </select>
                      </div>
                      <div class="clearfix"></div>
                      <div class="tbpaddingdiv1 text-center">
                        <button type="submit" class="btn btn-success">Add to payment</button>
                      </div>
                      <div class="clearfix"></div>
                </div>
            </div>
        </div

请帮帮我。

1 个答案:

答案 0 :(得分:0)

试试这个:

替换此代码

<select class="form-control">
   <option selected>Selected Vendor</option>
   <option>1</option>
   <option>2</option>
   <option>3</option>
</select>

<%= select_tag "User", options_from_collection_for_select(User.all, :id, :name), , {:class=>'form-control', :prompt => 'Selected Vendor'}  %>

了解更多详情:http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag