Ruby on Rails Thursday, June 30, 2016

I think I understand what you want now, this is a clean up of what you did but I dont think is what you want

def create
  @appointment = Appointment.where("date = ? && timeslot = ?", date,timeslot).first_or_initialize

  respond_to do |format|
    if @appointment.new_record? # this means you did NOT found an Appointment for the given date and timeslot
      format.html { redirect_to new_appointment_path, notice:
'Appointment was successfully created.' }
      format.json { render :show, status: :created, location:
@appointment }
    else # this means you did found an Appointment for the given date and timeslot
      format.html { redirect_to root_path, notice: 'That appointment
is not available, please choose again' } # this redirect works with no
notice
      format.js { render json: @appointment.errors, status:
:unprocessable_entity }
    end
  end
end

 now keep in mind that you are doing it wrong, 'create action' it for creating, what you need is 
 a contitional link that leads to either new or index action and leave create alone doing only what
 it should. So can you explain your goal as a user story, so that I can guide you to do it properly ?


On Thu, Jun 30, 2016 at 10:27 AM, Ruth Stephenson <lists@ruby-forum.com> wrote:
Hi, I have a very beginners understanding of rails and ruby and I keep
getting stuck. Some previous posts have helped me a great deal but I now
have a different issue and so I thought I should start another post with
a different topic.

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. I have created a method called isValid? in the model (See below)

I think the method is correct as the system is getting as far as the
redirect. The problem is, it keeps redirecting to the page I told it to
go to if it's not saved(the homepage or root_path). (Also the
appointments are not saving).

So, there now seems to be an issue with saving the appointment data to
the database.

If anyone could give me some pointers, I would be so, so grateful!

CODE:

appointments Model:
class Appointment < ActiveRecord::Base

  def isValid?
    taken= Appointment.where("date = ? && timeslot = ?", date,timeslot)
     Appointment.save unless taken
  end
end


appointments controller:

def create
    valid = @appointment = Appointment.new(appointment_params).isValid?

      respond_to do |format|
      if valid
        format.html { redirect_to new_appointment_path, notice:
'Appointment was successfully created.' }
        format.json { render :show, status: :created, location:
@appointment }
      else
        format.html { redirect_to root_path, notice: 'That appointment
is not available, please choose again' } # this redirect works with no
notice
        format.js { render json: @appointment.errors, status:
:unprocessable_entity }
      end
    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/48c5adc17a16f1c704752ad165f5c5cd%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 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/CANkJ5gnLruYMY5CF1Z0yxY5Hn0Mk-ma6EBneUskbKT6SNQZK8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment