0
我有一個簡單明瞭的簡單要求。 Donations
是Church
的子女。rails嵌套屬性窗體重複記錄數
教堂 - 模型
class Church < ActiveRecord::Base
belongs_to :user, :class_name => 'User', :foreign_key => 'user_id'
has_many :donations
accepts_nested_attributes_for :donations
end
教堂 - 控制器
class ChurchesController < ApplicationController
def show
# @church = Church.friendly.find(params[:id])
@church = Church.find(params[:id])
donations = @church.donations.build
end
捐贈 - 模型
class Donation < ActiveRecord::Base
belongs_to :church, :class_name => 'Church', :foreign_key => 'church_id'
end
這裏是issue.What我有鑑於/教堂/ show.erb
<%= form_for(@church ,url: church_path(@church), html: { method: :put }) do |f| %>
<%= f.fields_for :donations do |d| %>
<div class="content">...</div>
<% end %>
<% end %>
現在我的麻煩是div.content正在重複保存的「n」個捐贈記錄。請幫忙
你可以附上截圖嗎? – Pavan
如果你不想這樣做,爲什麼不把你的div.content放在fields_for塊之外? –
但我需要保存模型數據的實際形式看起來像 <%= form_for(@church,url:church_path(@ church),html:{method :: put})do | f | %> <%= f.fields_for:donations do | d | %> <%= d.text_field:name,:placeholder =>「卡上的名稱」,:class =>「form-control」,:required => true%> –