我在這方面很難找到信息,但認爲解決方案很簡單。總之,我需要能夠一次向一個模型添加多個條目。用戶故事是這樣的:用戶選擇「添加新的」並且被引導到他們可以簡單地添加一個條目或者選擇他們想要添加的期望條目的下拉的頁面。在一個模型上創建多個條目 - Ruby on Rails(非嵌套)
我看到的所有帖子都有關於使用嵌套對象的信息,但我只使用了一個模型。我是否需要遵循相同的協議?有一種更簡單的方法嗎?自從剛入門Ruby之後,我只是在尋找錯誤的術語嗎?
基本的應用程序是這樣的:
ticket_controller.rb
def new
@ticket = Ticket.new
end
def create
tnum = gets.chomp
tnum.times do Ticket.new(ticket_params)
respond_to do |format|
if @ticket.save
format.html { redirect_to @ticket, notice: 'Ticket was successfully created.' }
format.json { render action: 'show', status: :created, location: @ticket }
else
format.html { render action: 'new' }
format.json { render json: @ticket.errors, status: :unprocessable_entity }
end
end
end
new.html.erb
<h1>New ticket</h1>
<%= render 'form' %>
<%= link_to 'Back', tickets_path %>
我已經看過整個網站,只是覺得我失去了一些東西!感謝您指點我需要的方向。
_form.html.erb
<%= form_for(@ticket) do |f| %>
<% tnum.times do |index|%>
<div class="field">
<%= f.label :type %><br>
<%= f.text_field :type %>
</div>
<div class="field">
<%= f.label :amount %><br>
<%= f.text_field :amount %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
缺少模板票據/創建,應用程序/創建是我現在收到的唯一錯誤,在此工作並將分享我找到的!再次感謝你的幫助! – nathanielelliott
這個過程似乎正在失去陣列。有沒有人看過這個呢? – nathanielelliott