2012-01-05 23 views
0

**大家好,我正在嘗試構建一個地址簿,用戶可以在其中插入他的詳細信息。如何在表單中將動態生成的字段添加到數據庫導軌

  1. 用戶可以從表單中插入他的信息,如果他想添加更多的信息,比如數字可以是家庭和個人類型,我可以提供動態字段。 我的查詢是如何綁定到導軌形式的動態值和提交數據到數據庫。**

我的模型看起來像一個用戶可以具有信息類型信息的姓名和信息數據 的數據例如用戶可以插入一個信息類型=數字infoname = HOMEPHONE INFODATA = 23623623用戶可以插入他想要的不同type.Please幫助
我做它像這樣

<%= form_for @client_info, :validate => true do |f|%> 
<% if @client_info.errors.any? %> 
<div id="error_explanation", class="alert-message Warning"> 
    <h2><%= pluralize(@client.errors.count, "error") %> Occured</h2> 
    <ul> 
     <% @client.errors.full_messages.each do |msg| %> 
     <li> 
      <%= msg %> 
     </li> 
     <% end %> 
    </ul> 
</div> 
<% end %> 
<select id = "opts" > 
    <option value = "0">Select Option</option> 
    <option value = "1">Phone Number</option> 
    <option value = "2">Address</option> 
    <option value = "3">Date</option> 
    <option value = "4">Email</option> 
    <option value = "5">Link</option> 
</select> 
<form> 
    <div id="ap"></div> 
</form> 
<script type="text/javascript"> 
    $(document).ready(function() { 

     $("#opts").change(function() { 
      if($("#opts").val() == ("1")) { 
       $("#ap").append('<div id="phn"><select id ="phn"><option>Personal</option><option >Home</option><option >Custom</option></select> <input type ="text" id="ad"></input>' + '<a href="#" class="remove1" id="rm" >Remove</a></div>'); 
      } 
      if($("#opts").val() == ("2")) { 
       $("#ap").append('<div id= "add"><select id ="add"><option>Home Address</option><option >Work Address</option><option >Custom</option></select> <textarea rows="2" cols="20"></textarea>' + '<a href="#" class="remove2" id="rm" >Remove</a></div>'); 
      } 
      if($("#opts").val() == ("3")) { 
       $("#ap").append('<div id ="date"><select id ="date"><option>Birthday</option><option >Anniversary</option><option >Custom</option></select> <input type ="text" id="datepic" ></input>' + '<a href="#" class="remove3" id="rm" >Remove</a></div>'); 
      } 
      if($("#opts").val() == ("4")) { 
       $("#ap").append('<div id ="email"><select id ="email"><option>Home Email</option><option >Work Email</option><option >Custom Email</option></select> <input type ="text"></input>' + '<a href="#" class="remove4" id="rm" >Remove</a></div>'); 
      } 
      if($("#opts").val() == ("5")) { 
       $("#ap").append('<div id="link"><select id ="link"><option>Profle</option><option >Blog</option><option>Homepage</option><option>Custom</option></select> <input type ="text"></input>' + '<a href="#" class="remove5" id="rm" >Remove</a></div>'); 
      } 

     }); 
     $('.remove1').live('click', function() { 
      $("#phn").remove(); 
      return false; 
     }); 

    }); 

</script> 
<div class="actions"> 
    <%= f.submit :class => "btn primary" %> <a href="<%= clients_path %>" class="btn">Cancel</a> 
</div> 
<% end %> 

現在儘可能多的數字我想要添加這個html生成的字段到我的rails形成的?

回答

0

所有的輸入都將作爲參數傳遞,您可以循環遍歷它們,並在創建操作中將它們務實地保存在Controller中。

0

我把號碼一個單獨的模型與它們之間的一個一對多的關係,這樣的事情:

class User 
    has_many :user_infos 
end 

class UserInfo 
    belongs_to :user 

    # attributes: 
    # user_id (what user this info belongs to) 
    # type (could be Number, Email, whatever) 
    # name (an identifier like "home phone", "work email" etc) 
    # data (the actual value) 
end 

然後你可以再補充一個控制器爲UserInfo模型在那裏你可以添加信息給用戶。

+0

我用這種方法做了,現在我只想用一個模型文件做..感謝您的回覆。 – 2012-01-05 09:23:31

+0

然後我想你會使用更合適的數據庫引擎,如基於文檔的數據庫(例如mongodb) ,或者只是將user_infos的序列化數組存儲在用戶模型的文本字段中。儘管如此,第二個選項使得嘗試實現「按數字搜索」功能或類似功能變得很麻煩。 – Frost 2012-01-05 09:29:59

+0

嘿馬丁你可以請再看看我的問題再次我已經添加了代碼並做了一些更改.. – 2012-01-05 10:09:41

相關問題