如何在预订表格上致电服务?Ruby on Rails



所以我有客户页面,服务页面并使用可预订的宝石。因此,目前当我单击客户名称旁边的"预订"时,会弹出一个预订表格,允许我选择日期和时间以及约会的长度。但是,使用此表格,我还希望能够选择约会所需的服务,因此一旦添加了服务,预订表单上的下拉列表将显示已添加的服务。(希望这是有道理的(

有人可以帮我解决这个问题吗?

预订 (_form.html.erb(

<%= form_for([@client, @booking]) do |f| %>
  <p>
    <%= f.label 'start_time', 'Start time' %>
    <%= f.datetime_select :start_time, { minute_step: 15 } %>
  </p>
  <p>
    <%= f.label 'length', 'Length of booking in hours' %>
    <%= f.number_field 'length', min: 1 %>
  </p>
  <%= f.submit 'Submit' %>
<% end %>

Services (index.html.erb(

<h1>Services <%= link_to "+ New", new_service_path %></h1>
<table>
<div class="row">
  <div class="hidden-xs col-sm-3">
    <h3>Name</h3>
  </div>
  <div class="hidden-xs col-sm-3">
    <h3>Description</h3>
  </div>
  <div class="hidden-xs col-sm-3">
    <h3>Price</h3>
  </div>
  <div class="hidden-xs col-sm-3">
    <h3>Service Duration</h3>
  </div>
</div>
  <tbody>
    <% @services.each do |service| %>
      <tr>
        <td><%= service.name %></td>
        <td class="tb1"><%= service.description %></td>
        <td class="tb2"><%= service.price %></td>
        <td class="tb3"><%= service.duration %></td>
        <td><%= link_to 'Show', service %></td>
        <td><%= link_to 'Edit', edit_service_path(service) %></td>
        <td><%= link_to 'Destroy', service, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

bookings_controller.rb

class BookingsController < ApplicationController
  respond_to :html, :xml, :json
  before_action :find_client
  def index
    @bookings = Booking.where("client_id = ? AND end_time >= ?", @client.id, Time.now).order(:start_time)
    respond_with @bookings
  end
  def new
    @booking = Booking.new(client_id: @client.id)
  end
  def create
    @booking =  Booking.new(params[:booking].permit(:client_id, :start_time, :length))
    @booking.client = @client
    if @booking.save
      redirect_to client_bookings_path(@client, method: :get)
    else
      render 'new'
    end
  end
  def show
    @booking = Booking.find(params[:id])
  end
  def destroy
    @booking = Booking.find(params[:id]).destroy
    if @booking.destroy
      flash[:notice] = "Booking: #{@booking.start_time.strftime('%e %b %Y %H:%M%p')} to #{@booking.end_time.strftime('%e %b %Y %H:%M%p')} deleted"
      redirect_to client_bookings_path(@client)
    else
      render 'index'
    end
  end
  def edit
    @booking = Booking.find(params[:id])
  end
  def update
    @booking = Booking.find(params[:id])
    # @booking.clients = @clients
    if @booking.update(params[:booking].permit(:client_id, :start_time, :length))
      flash[:notice] = 'Your booking was updated succesfully'
      if request.xhr?
        render json: {status: :success}.to_json
      else
        redirect_to client_bookings_path(@client)
      end
    else
      render 'edit'
    end
  end
  private
  def save booking
    if @booking.save
        flash[:notice] = 'booking added'
        redirect_to client_booking_path(@client, @booking)
      else
        render 'new'
      end
  end
  def find_client
    if params[:client_id]
      @client = Client.find_by_id(params[:client_id])
    end
  end
end

services_controller.rb

class ServicesController < ApplicationController
  before_action :set_service, only: [:show, :edit, :update, :destroy]
  # GET /services
  # GET /services.json
  def index
    @services = Service.all
  end
  # GET /services/1
  # GET /services/1.json
  def show
  end
  # GET /services/new
  def new
    @service = Service.new
  end
  # GET /services/1/edit
  def edit
  end
  # POST /services
  # POST /services.json
  def create
    @service = Service.new(service_params)
    respond_to do |format|
      if @service.save
        format.html { redirect_to @service, notice: 'Service was successfully created.' }
        format.json { render :show, status: :created, location: @service }
      else
        format.html { render :new }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /services/1
  # PATCH/PUT /services/1.json
  def update
    respond_to do |format|
      if @service.update(service_params)
        format.html { redirect_to @service, notice: 'Service was successfully updated.' }
        format.json { render :show, status: :ok, location: @service }
      else
        format.html { render :edit }
        format.json { render json: @service.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /services/1
  # DELETE /services/1.json
  def destroy
    @service.destroy
    respond_to do |format|
      format.html { redirect_to services_url, notice: 'Service was successfully destroyed.' }
      format.json { head :no_content }
    end
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_service
      @service = Service.find(params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def service_params
      params.require(:service).permit(:name, :description, :price, :duration)
    end
end

据我所知,您想要在预订表单(即新操作(上显示一个包含所有服务的下拉列表,以便可以选择它。然后,当他们提交此服务时,他们将该服务添加到他们的预订中?

首先,您需要将service_id作为列添加到您的预订表和Booking类中的关联中。我建议对协会使用专门的迁移:references例如:

class AddServiceToBookings < ActiveRecord::Migration[5.0]
  def change
    add_reference :bookings, :service, foreign_key: true
  end
end

然后将关联添加到您的预订舱位中是个好主意:

class Booking < ActiveRecord::Base
  belongs_to :service

然后,您可以使用collection_select在表单中的服务下拉列表中显示服务集合:

<%= form_for([@client, @booking]) do |f| %>
  <p>
    <%= f.label 'start_time', 'Start time' %>
    <%= f.datetime_select :start_time, { minute_step: 15 } %>
  </p>
  <p>
    <%= f.label 'length', 'Length of booking in hours' %>
    <%= f.number_field 'length', min: 1 %>
  </p>
  <p>
    <%= f.label 'service_id', 'Service' %>
    <%= f.collection_select :service_id, Service.all, :id, :name %>
  </p>
  <%= f.submit 'Submit' %>
<% end %>

然后,您需要在预订控制器的许可证/要求部分中允许service_id

@booking =  Booking.new(params[:booking].permit(:client_id, :service_id, :start_time, :length))

您可能需要在这里和那里进行更多调整,但这就是它的要点。

最新更新