2015-03-08 82 views
0

你好,我是一個新的紅寶石在rails.I試圖保存在數據庫中選擇標記的值,但它不會被保存。有人可以告訴我我做錯了什麼。我是否正確使用表格中的選擇標籤選擇標記選項沒有被保存在數據庫

<%= link_to("Back" , {:action => 'rpage'}) %> 
<%= form_for(:request , :url => {:action => 'servicecreate'}) do |f| %> 
    <div class="field"> 
    <%= f.label :when %><br> 
    <%= f.text_field :when %> 
    </div> 
    <div class="field"> 
    <%= f.label :where %><br> 
    <%= f.text_field :where %> 
    </div> 
    <div class="field"> 
    <%= f.label :what %><br> 
    <%= f.text_field :what %> 
    </div> 
    <!--<%= f.select :category, @categories.each do |article| article end %>--> 
    <select name=:category> 
    <% @categories.each do |author| %> 
     <option value="<%= author %>"><%= author %></option> 
    <% end %> 
    </select> 

    <div class="field"> 
    <%= f.label :negotiable %><br> 
    <%= f.text_field :negotiable %> 
    </div> 

    <div class="field"> 
    <%= f.label :price %><br> 
    <%= f.text_field :price %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
+0

改爲使用collection_select標記。 – sansarp 2015-03-08 17:12:43

+0

你可以引用我的鏈接,我試圖瞭解它,但我不能 – Gardezi 2015-03-08 17:13:48

回答

0

請參考我在項目中實施的以下用法。

<%= collection_select :user, :user_id, User.all, :id, :first_name, 
{:prompt => 'Please Select the User'} %> 

而且資源可以在 Rails FormOptionsHelper

+0

詹姆斯你可以替換:用戶與:category,:user_id與:category_id,User.all與@categories和:first_name與作者。希望它現在可以工作。 – sansarp 2015-03-08 17:30:49

0

發現你似乎已經註釋掉f.select形式幫手 - 有一個明確的<select>標籤替換它來代替。但是,Rails期望通過使用其表單助手來強制執行特定的字段命名約定。在這種情況下,<select>標籤名稱應爲request[category]

所以,如果你曾經使用

f.select :category, @categories 

Rails的方法生成的HTML(假設@categories=['a','bb','ccc']):

<select name="request[category]" id="request_category"> 
    <option value="a">a</option> 
    <option value="bb">bb</option> 
    <option value="ccc">ccc</option> 
</select> 

另一個問題可能是,category字段不是白名單中的控制器。例如。你應該有類似

params.require(:request).permit(:when, :where, :what, :category, :negotiable, :price) 

檢查Rails的日誌文件(顯示在屏幕上的發展模式)的「類別不允許」的消息。