2015-01-14 56 views

回答

3

您可以用以下解決方案

嘗試在查看文件

<%= form_tag your_action_path do %> 
    <% 4.times do |i|%> 
    Content : <%= text_area_tag :thought_content, "", :name => "thoughts[][content]" %> 
    Author : <%= text_field_tag :thought_author, "", :name => "thoughts[][author]" %> 
    <% end %> 
    <%= submit_tag "Submit" %> 
<% end %> 

控制器代碼:

def your_action 
    params[:thoughts].each do |thought_params| 
    Thought.create(thought_params) 
    end 
    ### 
    #Any further code# 
    ### 
end 

希望它爲你工作:)

+0

謝謝,它工作。 :) –

3

在前端,你可以使用jQuery的onClick函數來添加更多的思想領域,即你可以添加一個名爲鏈接「添加更多」 &創建一個jQuery函數相同的形式動態字段名&中添加字段的另一個想法您可以使用後端

@thoughts = Thought.create([{ author: 'Chicago', content: 'content' }, { author: 'Chicago', content: 'content' }, .......]) 

一次創建多個條目。

+1

尼斯一個班輪解決方案! –

相關問題