here are some tips, note that I did not run the code so there might be
syntax errors


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

  def index
    # load_appointments can be used here
    @appointments = Appointment.all
  end

  def show
  end

  def new
    @appointment = Appointment.new
  end

  def edit
  end

  def create
    @appointment = Appointment.new(appointment_params)
    # @appointments has not been loaded, is nil, you will get an error here
    # you can add a before_action filter and load it there
    @appointments.find(params[:date, :timeslot]) # dont load this here
since is not always needed

    # do not use this 'and' litely it has a different precedenca than '&&''
    # you should move this logic to the model, the controller should not be
concerned
    # with persistence logic, look
    respond_to do |format|
      unless @appointments.has_date_and_timeslot?
        # some where in your model
        # def has_date_and_timeslot?
        #  date.present? && timeslot.present?
        # end
        # or if you want to use 'if' instead of 'unless'
        # def missing_date_and_timeslot?
        #  date.nil? && timeslot.nil?
        # end


        if @appointment.save
          format.html { redirect_to @appointment, notice: 'Appointment
was successfully created.' }
          format.json { render :show, status: :created, location:
@appointment }
        else #elsif # elsif what? you meant 'else'?
          format.html { render :new }
          format.json { render json: @appointment.errors, status:
:unprocessable_entity }
        end
      else # this else was missing I think
        # here you already called render, you cant redirect, and I think
you are missing an else
        # also you load appointment above only for this to work so move
that load here, dont use
        # a before filter since this will not always be need
        # load_appointments
        redirect_to page_home_path
      end
    end
  end

  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

  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

    # def load_appointments
    #   # use a pagination game like carrierwave or will_paginate
    #   @appointments = Appointment.all
    # 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

On Wed, Jun 29, 2016 at 12:13 PM, Ruth Stephenson <[email protected]>
wrote:

> 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 [email protected].
> To post to this group, send email to [email protected].
> 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.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CANkJ5gki%3D9cLUriBTAkibLADujQVjRvK%3DL9m8BCf%3DT_F2LxZSA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to