如何在日历上显示预约? Ruby on Rails

时间:2018-03-25 17:33:33

标签: ruby-on-rails

所以我一直在关注Dakota Lee Martinez Appointment项目,试图让它适应我的项目(我的项目是预订系统,为客户预约。我一直试图让我的约会显示在日历上就像在他的日历上一样,但是我对我应该怎么样感到有些困惑。我已经多次尝试但是一直在犯错误。

(使用gem" simple_calendar","〜> 2.0"显示日历)

错误:

NoMethodError in AppointmentsController#index

undefined method `upcoming_appointments' for nil:NilClass
Rails.root: C:/Sites/booking_saloniser - Calendar

Application Trace | Framework Trace | Full Trace

app/controllers/appointments_controller.rb:6:in `index'

Dakota页面:https://medium.com/@dakotaleemartinez/rails-appointments-project-4a16eaf1f7ca

Dakota Repo:https://github.com/DakotaLMartinez/rails-appointments

new.html.erb

<div class="row">
 <div class="col-md-4">
  <h1>New Appointment</h1>
  <%= render partial: 'form', locals: { appointment: @appointment, client: @client } %>
 </div>  
</div>

index.html.erb

<h1>Appointments <%= link_to "+ New", new_appointment_path %></h1>

<%= add_weekly_calendar(@appointments) %>

_form.html.erb

<%= render partial: "layouts/errors", locals: { object: appointment } %>
<%= form_for appointment do |f| %>
  <div class="field">
    <p>
      <%= label_tag "appointment[appointment_time][date]", "Date" %>
      <%= text_field_tag "appointment[appointment_time][date]", appointment_date(appointment), class: "datepicker" %>
    </p>
  </div>
  <div class="field">
    <p id="time">
      <%= label_tag "appointment[appointment_time][hour]", "Time" %>
      <%= hour_selector("appointment[appointment_time][hour]", appointment) %> : 
      <%= min_selector("appointment[appointment_time][min]", appointment) %> 
    </p>
 </div>
 <div class="field">
   <p>
     <%= f.label :duration %> 
     <%= duration_hour_field("appointment[duration][hour]", appointment) %>
     <%= duration_minute_field("appointment[duration][min]", appointment) %>
   </p>
 </div>
 <div class="field">
   <p>
     <%= f.label :price %> 
     <%= f.number_field :price, step: 0.25, value: float_two_decimals(appointment.price) %>
   </p>  
 </div>
 <p><%= f.submit %></p>
<% end %>

appointment.rb

class Appointment < ActiveRecord::Base
belongs_to :user
belongs_to :client
accepts_nested_attributes_for :client

def start_time
  self.appointment_time
end

def end_time
  appointment_time + duration.seconds
end

def client_name 
  client.name
end

## Form Parsing methods

def client_attributes=(atts)
  if atts[:name] != ""
    self.client = self.user.clients.find_or_create_by(atts)   
  end
end

def appointment_time=(time)
  if time.is_a?(Hash)
    self[:appointment_time] = parse_datetime(time) 
  else
    self[:appointment_time] = time
  end
end

def parse_date(string)
  array = string.split("/")
  first_item = array.pop
  array.unshift(first_item).join("-")
end

def parse_datetime(hash)
  if hash["date"].match(/\d{2}\/\d{2}\/\d{4}/)
    Time.zone.parse("#{parse_date(hash["date"])} #{hash["hour"]}:#{hash["min"]}")
  end
end

def duration=(duration) 
  if duration.is_a?(Hash)
    self[:duration] = parse_duration(duration)
  else 
    self[:duration] = duration
  end
end

def parse_duration(hash)
  hash["hour"].to_i + hash["min"].to_i
end
end

appointments_controller.rb

class AppointmentsController < ApplicationController
before_action :set_appointment, only: [:show, :edit, :update, :destroy]
# GET /appointments
# GET /appointments.json
def index
  @upcoming_appointments = current_user.upcoming_appointments
end

# GET /appointments/1
# GET /appointments/1.json
def show

end

# GET /appointments/new
def new
  @appointments = current_user.appointments.select { |a| a.persisted? }
  @appointment = current_user.appointments.build
end

# GET /appointments/1/edit
def edit
end

# POST /appointments
# POST /appointments.json
def create
  @appointment = Appointment.new(appointment_params)

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

# PATCH/PUT /appointments/1
# PATCH/PUT /appointments/1.json
def update
  respond_to do |format|
    if @appointment.update(appointment_params)
      format.html { redirect_to @appointment, notice: 'Appointment was successfully updated.' }
      format.json { render :show, status: :ok, location: @appointment }
    else
      format.html { render :edit }
      format.json { render json: @appointment.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /appointments/1
# DELETE /appointments/1.json
def destroy
  @appointment.destroy
  respond_to do |format|
    format.html { redirect_to appointments_url, notice: 'Appointment was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
def set_appointment
  @appointment = current_user.appointments.find_by(id: params[:id])
  if @appointment.nil? 
    flash[:error] = "Appointment not found."
    redirect_to appointments_path
  end
end

  # Never trust parameters from the scary internet, only allow the white list through.
def appointment_params
    params.require(:appointment).permit(:appointment_time, :duration, :price, :user_id, :client_id)
  end

有没有人有机会帮助我如何做达科塔在他的项目上所做的事情?我尝试过搜索不同的宝石等来帮助我这样做,但我喜欢Dakota所做的事情。

0 个答案:

没有答案