Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to turn dropdown days into calendar form


  • Please log in to reply
No replies to this topic

#1 RBG54

RBG54

    Passenger

  • Members
  • 3 posts

Posted 19 May 2014 - 07:28 PM

I have a Tour, Day, and Reservation model. In my Tour controller, I can create a tour and manually enter in days that the tour is available on (Monday, Tuesday, etc). I am then displaying those days in a drop down in tours/id/reservation/new.

My goal is to figure out a way to turn the days into more of a calendar form; where it shows the days and associated dates, so it's easier for customers to tell when they're booking a tour. If a tour is available on Monday and Tuesday, it's available every Monday and Tuesday throughout the month. I want to show the day, its associated date in every month, and the year. Any ideas? Thank you in advance!

class Day < ActiveRecord::Base
belongs_to :tour
has_many :reservations
accepts_nested_attributes_for :reservations
end

class Reservation < ActiveRecord::Base
belongs_to :tour
belongs_to :day
accepts_nested_attributes_for :day
end

class Tour < ActiveRecord::Base
has_many :reservations
has_many :days, dependent: :destroy
accepts_nested_attributes_for :days, allow_destroy: true
end

. RESERVATION CONTROLLER:

class ReservationsController < ApplicationController
before_action :require_admin, only: [:show]


def new
@reservation = Reservation.new
@tour = Tour.includes(:days).find(params[:tour_id])
end

def create
@tour = Tour.includes(:days).find(params[:tour_id])
@reservation = Reservation.new(reservation_params)

if @reservation.save
Stripe.api_key = ''
Stripe::Charge.create(
:amount => @tour.amount, # amount in cents, again
:currency => "usd",
:card => params[:stripeToken]
)

flash[:success] = "Your reservation has been booked for #{@reservation.passengers} person(s). Please save this info."
redirect_to new_tour_reservation_path(@tour)
else
render 'new'
end
end

def show

end

private

def reservation_params
params.require(:reservation).permit(:passengers, :home_pickup, :home_dropoff, :home_address, :zipcode, :telephone_number, :amount, day_attributes: [:id, :name, :_destroy])
end
end

TOURS CONTROLLER:

class ToursController < ApplicationController
before_action :require_admin

def index
@tours = Tour.all
end

def new
@tour = Tour.new
2.times { @tour.days.build }
end

def create
@tour = Tour.new(tours_params)
if @tour.save
flash[:success] = "Tour #{@tour.name} has been successfully added."
redirect_to new_tour_path
else
flash[:error] = "The tour #{@tour.name} was not successfully saved. Please try again"
render 'new'
end
end

def show
@tour = Tour.find_by(id: params[:id])
@reservation = Reservation.new
end

def edit
@tour = Tour.find_by(id: params[:id])
end

def update
@tour = Tour.find_by(id: params[:id])
if @tour.update_attributes(tours_params)
flash[:success] = "#{@tour.name} has been successfully updated."
redirect_to tours_path
else
flash[:error] = "#{@tour.name} has not been updated. Please try again."
render 'edit'
end
end

def delete
@tour = Tour.find_by(id: params[:id])
end

def destroy
@tour = Tour.find_by(id: params[:id])
if @tour.destroy
flash[:success] = "The #{@tour.name} has been successfully deleted."
redirect_to tours_path
else
flash[:error] = "The #{@tour.name} has not been deleted. Please try again."
render 'edit'
end
end

private

def tours_params
params.require(:tour).permit(:name, :amount, :days, :time, :location, :duration, :description, :highlights, :available_spots, days_attributes: [:id, :name, :_destroy])
end
end

RESERVATION #NEW VIEW:

.container
.row
.col-md-8
%h2
= @tour.name
Available spots: #{@tour.available_spots}
%p= @tour.description
%h4 Information:
%ul
%li
Location:#{@tour.location}
%li
Duration:#{@tour.duration}
%h4 Departure point and time:
%ul
%li
Free pickup from #{@tour.location} at #{@tour.time}
%h4 Amenities
%h4 Additional info
.col-md-4
%h2 Book this tour
%p
Pricing is for both adults and children. Please provide your own car seat.
%p
%h3
= @tour.amount
= bootstrap_form_for([@tour, @reservation], html: { id: 'payment-form'}) do |f|
= f.select :passengers, options_for_select([*1..10])
- options_array = []
- @tour.days.each do |day|
- options_array << [day.name, day.id]
= f.select :day, options_for_select(options_array, 1)
%h4 Would you like to be picked up straight from home?
= f.check_box :home_pickup, label: 'Pickup: Additional $15.00'
= f.check_box :home_dropoff, label: 'Dropoff: Additional $15.00'
= f.text_field :home_address
= f.text_field :zipcode
= f.text_field :telephone_number
%span.payment-errors
.form-group
= label_tag :card_number, 'Credit card number:'
= text_field_tag :card_number, nil, name: nil, class: 'form-control', data: {stripe: 'number'}
.form-group
= label_tag :security_code, 'Security code:'
= text_field_tag :security_code, nil, name: nil, class: 'form-control', data: {stripe: 'cvc'}
.form-group
= label_tag :exp_date, 'Expiration:'
.form-group
= select_month(Date.today, {add_month_numbers: true}, class: 'form-control', data: {stripe: 'exp-month'})
= select_year(Date.today.year, {start_year: Date.today.year, end_year: Date.today.year + 4}, class: 'form-control', data: {stripe: 'exp-year'})
.form-group
= f.submit 'Book tour!', class: 'btn btn-success'

TOURS #NEW VIEW:

.container
.row
.col-md-3
%h2 Add a new tour
= bootstrap_form_for(@tour) do |f|
= f.text_field :name
= f.fields_for :days do |builder|
= builder.label :name, 'Days of Tour:'
= builder.text_field :name, label: 'Days of Tour:'
= builder.check_box :_destroy
= builder.label :_destroy, 'Remove day'
= f.text_field :amount
= f.text_field :available_spots
= f.text_field :time
= f.text_field :location
= f.text_field :duration
= f.text_area :description
= f.text_field :highlights
= f.text_field :highlights
= f.text_field :highlights
= f.text_field :highlights
= f.submit 'Add this tour', class: 'btn btn-success'

ROUTES:

resources :tours do
resources :reservations
end
 

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users