2015-04-23 33 views
0

我想將db值設置爲選擇選項標記並使用Rails 3將其顯示在下拉列表中。假設用戶模型有5個字段,並且我需要用戶模型的所有名稱列值將在選擇選項標籤中設置爲下拉列表。當頁面將被加載時(即-home.html.erb),所有名稱字段值應從數據庫中提取並設置在選擇標籤中。請幫助我解決此問題。提取數據庫值以使用Rails 3選擇選項標記3

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 

請幫助我。

+0

你如何使這個'home'模板?你想在表單中使用下拉菜單嗎? –

+0

@Gagan:第一頁是首頁。在數據庫之前的數據庫中。當用戶打開此頁面時,只有用戶模型的名稱值會顯示在下拉列表中。 –

+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