Ruby on Rails Wednesday, June 29, 2016

I'm sorry if this should be in the Ruby forum. I decided to put it here
as i'm developing in rails but I'm new to both. I am also wondering if
I'm putting the methods into the right files in rails, therefore I
thought to post in the rails forum.

I am making an appointment booking app and I have a very basic design. I
have created an appointments scaffold with name:string phone:string
email:string numpeople:integer date:date timeslot:string. On the view
for creating a new appointment I have stated that appointment 1 is
9-11am, appointment 2 is 12-2pm, appointment 3 is 3-5pm and appointment
4 is 5 - 7pm. The user is asked to enter 1,2,3 or 4.

When the user clicks on "make appointment" I'm trying to interrupt the
appointments controller (create method) so that I can check if the
date&&timeslot are nil. if that is the case, the system should continue
on to create the appointment, if not then I want to redirect to
somewhere else.

My code is not working and I'm very stuck (as I said I'm very new to
this) Am I going about this in the correct way? I am aware this is not
the best way to go about it, but this is the simplist way I know when I
am not familiar with the language and framework, so please humour my
roundabout methods :)

Please can anyone give me some pointers. (The entire appointments
controller is mentioned at the bottom of this post, in case it is
required)

def create # what I have for create so far
@appointment = Appointment.new(appointment_params)
@appointments.find(params[:date, :timeslot])

if @appointments.date.nil? and @appointments.timeslot.nil?
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 }
elsif
format.html { render :new }
format.json { render json: @appointment.errors, status:
:unprocessable_entity }
end

redirect_to page_home_path
end
end
end



_______________________________________________________________________

class AppointmentsController < ApplicationController
before_action :set_appointment, only: [:show, :edit, :update,
:destroy]

# GET /appointments
# GET /appointments.json
def index
@appointments = Appointment.all
end

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

# GET /appointments/new
def new
@appointment = Appointment.new
end

# GET /appointments/1/edit
def edit
end

# POST /appointments
# POST /appointments.json
def create
@appointment = Appointment.new(appointment_params)
@appointments.find(params[:date, :timeslot])

if @appointments.date.nil? and @appointments.timeslot.nil?
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 }
elsif
format.html { render :new }
format.json { render json: @appointment.errors, status:
:unprocessable_entity }
end

redirect_to page_home_path
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 = Appointment.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the
white list through.
def appointment_params
params.require(:appointment).permit(:name, :phone, :email,
:numpeople, :date, :timeslot)
end
end

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f07db123c8f52fa9ec0a042f49a0a38a%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment