dimanche 15 mai 2016

ruby constraints, if selection = then?

i am working with a reservation system and i am having problem with the constraints.. i have a list of cottages with a number of cottages vacant.. my system is when the customer reserve a cottage the number of vacant cottage need to decrease.. who will i write the code of that? please help me i have here some codes i started... and an image to help you understand my explanation..

what i want to accomplish here is:

Before a reservation: Cottage Availability: 5

if i fill up information & select "small cottage" and create reservation the availability value will -1.

After a reservation: Cottage availability: 4

form.html.erb

<%= form_for(@reservation) do |f| %>

  <%= f.label :reservation_date %><br>
  <%= f.date_select :reservation_date %>

  <%= f.label :customer_name %><br>
  <%= f.text_field :customer_name %>

  <%= f.label :address%><br>
  <%= f.text_field :address %>

  <%= f.label :contact_no %><br>
  <%= f.text_field :contact_no %>

  <%= f.label :cottage_class %><br>
  <%= f.select :cottage_id, options_for_select( Cottage.all.map { |g| [g.name, g.id]}) %>

  <%= f.submit %>
<% end %> 

schema.rb

ActiveRecord::Schema.define(version: 20160514141006) do

  create_table "cottages", force: :cascade do |t|
    t.string   "name"
    t.string   "rates"
    t.integer  "availability"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

  create_table "reservations", force: :cascade do |t|
    t.date     "reservation_date"
    t.string   "customer_name"
    t.string   "address"
    t.string   "contact_no"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "cottage_id"
  end

  add_index "reservations", ["cottage_id"], name: "index_reservations_on_cottage_id"

end

reservation.rb

class Reservation < ActiveRecord::Base
  validates :customer_name, :presence => true
  validates :address, :presence => true
  validates :contact_no, :presence => true

  belongs_to :cottage
end

cottage.rb

class Cottage < ActiveRecord::Base
  has_many :reservations
end

Form and list of cottages existing on the database

Aucun commentaire:

Enregistrer un commentaire