简体   繁体   中英

Form for mongoid embeds_many document

Currently I have two models.

class Booking
  include Mongoid::Document
  include Mongoid::Timestamps
  field :capacity, type: Integer
  field :date, type:Date

  embeds_many :appointments
  attr_accessible :capacity, :date, :appointments_attributes
  accepts_nested_attributes_for :appointments
end

class Appointment
  include Mongoid::Document
  field :name, type: String
  field :mobile, type: String

  embedded_in :booking
end

How do I create a form for the appointment which is embedded in booking? Ideally, I want to be able to add appointments to booking by specifying the booking date.

I can do it in the console, but can't figure out a way to create a form for that.

I have tried this, which doesn't work

<%= form_for(@booking) do |f| %>
  <div class="field">
    <%= f.label :capacity %><br />
    <%= f.text_field :capacity %>
  </div>
  <div class="field">
    <%= f.label :date %><br />
    <%= f.text_field :date %>
  </div>

  <h1>  form for appointment </h1>
        <%= f.fields_for :appointments do |builder| %>
          <div class="field">
            <%= builder.label :name %><br />
            <%= builder.text_field :name %>
          </div>
          <div class="field">
            <%= builder.label :mobile %><br />
            <%= builder.text_field :mobile %>
          </div>

        <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

in booking controller

  def create
    @booking = Booking.new(params[:booking])
    @appointment = @booking.appointments.build(params[:appointments])

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

Console command to do what the form is suppose to do:

book = Booking.create(capacity: 10, date: Date.today)
book.appointments.create(name: "Josh", mobile: "19923")
book.appointments.create(name: "Bosh", mobile: "123344")
book.appointments
 => [#<Appointment _id: 51a083c332213f2cc9000002, _type: nil, name: "Josh", mobile: "19923">, #<Appointment _id: 51a0840632213f2cc9000003, _type: nil, name: "Bosh", mobile: "123344">] 

So I solved the problem. Instead of using a nested form, I just create the Application document with it's own controller and form like usual. I add a date field to Application model and inside the controller, I query for the Booking document with that date and create the application with that booking.

here is the code:

  def create
    date = params[:appointment]['date']
    booking = Booking.find_or_create_by(date: date)
    @appointment = booking.appointments.new(params[:appointment])


    respond_to do |format|
      if @appointment.save
        format.html { redirect_to booking, notice: 'Appointment was successfully created.' }
        format.json { render json: @appointment, status: :created, location: @appointment }
      else
        format.html { render action: "new" }
        format.json { render json: @appointment.errors, status: :unprocessable_entity }
      end
    end
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM