使用嵌套形式 rails 创建 object

Creating a object with nested form rails

我有 3 个 table:成员、会议、meeting_has_members。 模型是:

member.rb

class Member < ActiveRecord::Base

 has_many :meeting_has_members
 has_many :meetings, through: :meeting_has_members

 accepts_nested_attributes_for :meeting_has_members
 accepts_nested_attributes_for :meetings

meeting.rb

class Meeting < ActiveRecord::Base

 has_many :meeting_has_members
 has_many :members, through: :meeting_has_members

 accepts_nested_attributes_for :meeting_has_members
 accepts_nested_attributes_for :members

meeting_has_member.rb

class MeetingHasMember < ActiveRecord::Base

 belongs_to :member
 belongs_to :meeting

我想做的是,在创建会议时,能够向其中添加成员,并在提交后创建关系 meeting_has_成员。 table meeting_has_members 有 2 列:member_id、meeting_id.

那就是 meeting_controller:

meeting_controller.rb

 class MeetingsController < ApplicationController

  before_action :set_meeting, only: [:show, :edit, :update, :destroy]

  def index
    @meetings = Meeting.all
  end

  def show
  end

  def new
    @meeting = Meeting.new
    @meeting_has_members = @meeting.meeting_has_members.build
  end

  def edit
  end

  def create
    @meeting = Meeting.new(meeting_params)

    respond_to do |format|
      if @meeting.save
        format.html { redirect_to @meeting, notice: 'Meeting was successfully created.' }
        format.json { render :show, status: :created, location: @meeting }
      else
        format.html { render :new }
        format.json { render json: @meeting.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @meeting.update(meeting_params)
        format.html { redirect_to @meeting, notice: 'Meeting was successfully updated.' }
        format.json { render :show, status: :ok, location: @meeting }
      else
        format.html { render :edit }
        format.json { render json: @meeting.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @meeting.destroy
    respond_to do |format|
      format.html { redirect_to meetings_url, notice: 'Meeting was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

  def set_meeting
    @meeting = Meeting.find(params[:id])
  end

  def meeting_params
    params.require(:meeting).permit(:meeting_name, :meeting_description, :meeting_date, :agenda_id,
                                  meeting_has_members_attributes: [:id],
                                  members_attributes: [:id])
  end

end

创建会议的表格是:

meetings/_form.html.erb

<%= form_for(@meeting) do |f| %>
  <% if @meeting.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@meeting.errors.count, "error") %> prohibited this meeting from being saved:</h2>

      <ul>
       <% @meeting.errors.full_messages.each do |message| %>
        <li><%= message %></li>
       <% end %>
      </ul>
    </div>
<% end %>

  <div class="field">
    <%= f.label :meeting_name %><br>
    <%= f.text_field :meeting_name %>
  </div>
  <div class="field">
    <%= f.label :meeting_description %><br>
    <%= f.text_field :meeting_description %>
  </div>
  <div class="field">
    <%= f.label :meeting_date %><br>
    <%= f.date_select :meeting_date %>
  </div>
  <div class="field">
    <%= f.label :agenda_id %>
    <%= f.select :agenda_id, Agenda.all.collect { |p| [p.agenda_name, p.id] } %>
  </div>
    <%= f.fields_for :meeting_has_members do |builder| %>
     <%= builder.label :member_id %>
     <%= builder.select :member_id, Member.all.collect { |m| [m.member_name, m.id] }%>
    <% end %>
  <div class="actions">
  <%= f.submit %>
  </div>
<% end %>

当我提交时,它会在 meeting_has_members table meeting_id 上创建,但它不会为 member_id 设置值,那应该是 select 嵌套形式。

谁能帮我解决这个问题?

你的meeting_params应该是这样的

def meeting_params
    params.require(:meeting).permit(:meeting_name, :meeting_description, :meeting_date, :agenda_id, meeting_has_members_attributes: [:id, :member_id])
end

请注意,我在 meeting_has_members_attributes 中添加了 :member_id 并删除了 members_attributes,因为您没有保存它们。