Can’t get this?..I’m close I think, my calculation appears to be
working, but… when i access duration = <%= @enquiry.course_duration
%>
in my final view it’s blank… course_duration is getting set to
null but works for the calculation?
controller…
class EnquiriesController < ApplicationController
layout ‘welcome’
before_filter :login_required, :except => [:new, :create, :blank]
def index
list
render :action => ‘list’
end
GETs should be safe (see
URIs, Addressability, and the use of HTTP GET and POST)
verify :method => :post, :only => [ :destroy, :create, :update ],
:redirect_to => { :action => :list }
def list
@enquiries = Enquiry.find(:all, :order => ‘created_at ASC’)
end
def list_by_start_date
@enquiries = Enquiry.find(:all, :order => ‘requested_start_date
ASC’)
end
def list_not_responded
# @enquiries = Enquiry.find(:all, :conditions => ‘responded = null’,
:order => ‘requested_start_date ASC’)
# entity = MyEntity.find_all_by_active(true)
@enquiries = Enquiry.find_all_by_responded(false)
end
def show
@enquiry = Enquiry.find(params[:id])
end
def new
@enquiry = Enquiry.new
@courses = Course.active_courses
@accomodations = Accomodation.active_accomodation
# @enquiry.course_booking_fee = Course.registration_fee
render :layout => "welcome"
end
def create
@enquiry = Enquiry.new(params[:enquiry])
@courses = Course.active_courses
@accomodations = Accomodation.active_accomodation
if @enquiry.save
@enquiry.set_fixed_fees # set the fixed fees
@enquiry.calculate_fees # calculate
@enquiry.save
flash[:notice] = 'Your enquiry has been sent to Europa School of
English. You have received a summary by email. We will contact you
shortly with further information.’
# redirect_to :action => ‘list’ rmoved rf, we need to go somewhere
else after a new enq.
redirect_to :action => 'blank', :id => @enquiry
# redirect_to :action => "blank"
Notifier.deliver_signup_thanks(@enquiry)
else
render :action => 'new'
end
end
def edit
@enquiry = Enquiry.find(params[:id])
@courses = Course.find(:all)
@accomodations = Accomodation.find(:all)
end
def update
@enquiry = Enquiry.find(params[:id])
if @enquiry.update_attributes(params[:enquiry])
flash[:notice] = ‘Enquiry was successfully updated.’
redirect_to :action => ‘show’, :id => @enquiry
else
render :action => ‘edit’
end
end
def destroy
Enquiry.find(params[:id]).destroy
redirect_to :action => ‘list’
end
def blank
@enquiry = Enquiry.find(params[:id])
end
end
model
class Enquiry < ActiveRecord::Base
belongs_to :course
belongs_to :accomodation
attr_accessor :accomodation_duration, :course_duration
before_validation :set_fixed_fees, :calculate_fees
def set_fixed_fees
# set accommodation_booking_fee from the Selected Accomodation
# set course_booking_fee from the Selected Course
self.course_booking_fee = course.registration_fee
self.accommodation_booking_fee = accomodation.booking_fee
end
def calculate_fees
# DO STUFF !
# self.blah_fee = blah_duration * whatever
self.course_fee = course.price_per_week * course_duration.to_i
self.accommodation_fee = accomodation.price_per_week *
accomodation_duration.to_i
@factor = case @duration_in_weeks
when 1..4
1.0
when 5..12
0.9
when 13..24
0.8
when 25..47
0.7
else # from 48 and up
0.5
end
self.course_fee = (self.course_fee * @factor)
self.total_fee = course_booking_fee + accommodation_booking_fee +
course_fee + accommodation_fee
end
validates_date :date_of_birth, :requested_start_date
validates_presence_of :title,
:firstname,
:surname,
:address1,
:phone,
:country,
:nationality,
:course_duration,
:english_level,
:course_id,
:accomodation_id,
:message => “can’t be blank”
validates_format_of :email, :with => RFC2822::EmailAddress
end