>我有3张桌子:成员,会议meeting_has_members。这些模型是:
会员.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
会议.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_member。该表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
创建会议的表单是:
会议/_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表meeting_id上创建,但它没有设置member_id的值,应该在嵌套形式中选择。
谁能帮我解决这个问题?
你的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
,因为您没有保存它们。