vendredi 19 juin 2015

Creating a object with nested form rails

I have 3 tables: members, meetings, meeting_has_members. The 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 to do is, while creating a meeting, be able to add members to it, and when submitted, create the relation meeting_has_member. The table meeting_has_members have 2 columns: member_id, meeting_id.

Thats the 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

And the form to create the meeting is:

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 %>

When I submit, it creates, on the meeting_has_members table meeting_id, but it doesn't set the value for the member_id, that should be select in the nested form.

Can anyone help me with this?

Aucun commentaire:

Enregistrer un commentaire