保存 parent_id 时保存 childs_ids - 嵌套视图 - collection_select (rails)

save childs_ids when saving parent_id - nested view - collection_select (rails)

我有两个模型:churchaddress 和 churchmember。我以他们自己的模式拯救教会成员。我想通过 form_for 教堂地址中的嵌套 fields_for 增加多个 select 教堂成员,并在保存教堂地址时将 churchaddress_id 保存在教堂成员模型上。最后,我想向教堂成员展示教堂地址,其中 churchmembers.churchaddress_id = churchaddress_id.

此时我在嵌套视图上有集合 select,但我无法在教堂地址 (parent) 上保存 selected 教会成员(孩子)。我希望有人能够帮助我。

# 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

提前感谢您的回复!

我终于设法解决了这个问题。花了我几个晚上,但结果很棒! 对于有同样问题的人,我有 post 下面的更改表格。

class Churchaddress < ActiveRecord::Base
    has_many :churchmembers, foreign_key: :churchaddress_id
    accepts_nested_attributes_for :churchmembers

    def to_s
        name
    end
end

<%= 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>
  <div class="churchmember_fields">
  <div class="field">    
    <%= collection_select(:churchaddress, :churchmember_ids, Churchmember.all, :id, :name, {:selected => @churchaddress.churchmember_ids, :include_hidden => false}, {multiple: true}) %>
  </div>
  </div>
  <br>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>