0
我確實有兩種模式:churchaddress和churchmember。我用自己的模式拯救教會成員。我想通過嵌套fields_for在form_for churchaddress中選擇多個churchmembers,並在我保存churchaddress時將churchaddress_id保存在教堂會員模型上。最後,我想向churchmembers.churchaddress_id = churchaddress_id的教會成員展示這個churchaddress。在保存parent_id時保存childs_ids - 嵌套視圖 - collection_select(rails)
在這一刻我有集合選擇嵌套視圖,但我不能設法保存在churchaddress(父母)選定的教會議員(孩子)。我希望有人能夠幫助我。
# churchaddress.rb model
class Churchaddress < ActiveRecord::Base
has_many :churchmembers, dependent: :destroy, autosave: true
accepts_nested_attributes_for :churchmembers,
reject_if: proc { |attributes| attributes['name'].blank? },
allow_destroy: true
validates_associated :churchmembers
validates :name,
presence: true
def to_s
name
end
end
# churchmember.rb model
class Churchmember < ActiveRecord::Base
belongs_to :churchaddress
accepts_nested_attributes_for :churchaddress
validates :name,
presence: true
def to_s
name
end
end
# churchaddresses controller
class ChurchaddressesController < ApplicationController
before_action :set_churchaddress, only: [:show, :edit, :update, :destroy]
def show
end
# GET /churchaddresses/new
def new
@churchaddress = Churchaddress.new
@churchaddress.churchmembers.build
end
# POST /churchaddresses
# POST /churchaddresses.json
def create
@churchaddress = Churchaddress.new(churchaddress_params)
respond_to do |format|
if @churchaddress.save
format.html { redirect_to @churchaddress, notice: 'Churchaddress was successfully created.' }
format.json { render :show, status: :created, location: @churchaddress }
else
format.html { render :new }
format.json { render json: @churchaddress.errors, status: :unprocessable_entity }
end
end
end
private
def set_churchaddress
@churchaddress = Churchaddress.find(params[:id])
end
def churchaddress_params
params.require(:churchaddress).permit(:name, churchmembers_attributes: [:id, :name, :_destroy, :churchmember_ids => [] ])
end
end
# form_for with nested fields_for churchaddresses
<%= form_for(@churchaddress) do |f| %>
<% if @churchaddress.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@churchaddress.errors.count, "error") %> prohibited this churchaddress from being saved:</h2>
<ul>
<% @churchaddress.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<h2>Church members</h2>
<%= f.fields_for :churchmembers, @churchaddress.churchmembers do |churchmember| %>
<div class="churchmember_fields">
<div class="field">
<%= churchmember.collection_select(:churchmember_ids, Churchmember.all, :id, :name, {include_blank: false, :include_hidden => false, :selected => @churchaddress.churchmembers.map(&:id)}, {:multiple => true}) %>
</div>
</div>
<% end %>
<br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
更新1:schema.rb
ActiveRecord::Schema.define(version: 20150909060826) do
create_table "churchaddresses", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "churchmembers", force: :cascade do |t|
t.string "name"
t.integer "churchaddress_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "churchmembers", ["churchaddress_id"], name: "index_churchmembers_on_churchaddress_id"
end
提前感謝您的答覆!
什麼類型的字段是'churchmember_ids'? – vee
@vee感謝您的回覆。我用schema.rb更新了我的問題。關於churchmember_ids:我沒有在我的數據庫中指定這個字段。我不在哪裏添加這個字段?你能幫助我嗎? –