ruby - Creating a object with nested form rails -
i have 3 tables: members, meetings, meeting_has_members. models are:
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
what i'm trying is, while creating meeting, able add members it, , when submitted, create relation meeting_has_member. table meeting_has_members have 2 columns: member_id, meeting_id.
thats 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 |format| if @meeting.save format.html { redirect_to @meeting, notice: 'meeting 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 |format| if @meeting.update(meeting_params) format.html { redirect_to @meeting, notice: 'meeting 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 |format| format.html { redirect_to meetings_url, notice: 'meeting 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
and form create meeting is:
meetings/_form.html.erb
<%= form_for(@meeting) |f| %> <% if @meeting.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@meeting.errors.count, "error") %> prohibited meeting being saved:</h2> <ul> <% @meeting.errors.full_messages.each |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 |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 %>
when submit, creates, on meeting_has_members table meeting_id, doesn't set value member_id, should select in nested form.
can me this?
your meeting_params
should
def meeting_params params.require(:meeting).permit(:meeting_name, :meeting_description, :meeting_date, :agenda_id, meeting_has_members_attributes: [:id, :member_id]) end
notice added :member_id
in meeting_has_members_attributes
, removed members_attributes
not saving them.
Comments
Post a Comment