I''ve got a few problems with this...Hopefully it''s cleaer what
I''m
trying to do (work out and display two prices, one discounted / one
full). Be grateful for answers...
The total price and discounted price are not calculated correctly and
the duration is not coming through to the output page.
---------------
class Course < ActiveRecord::Base
has_one :enquiry
attr_accessor :duration_in_weeks
def self.all_courses
find(:all)
end
def self.active_courses
# find(:all)
# find(:all, :conditions => ["active = ?", true], :order =>
''created_at DESC'', :limit => 5)
find(:all, :conditions => {:archive => false}, :order =>
''display_order ASC'', :limit => 100)
end
def self.old_courses
# find(:all)
# find(:all, :conditions => ["active = ?", true], :order =>
''created_at DESC'', :limit => 5)
find(:all, :conditions => {:archive => true}, :order =>
''created_at
DESC'', :limit => 100)
end
def discounted_price
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.6
end
total_price * factor
end
def total_price
(@duration_in_weeks * price_per_week) + registration_fee
end
def duration_in_weeks=(duration_in_weeks)
@duration_in_weeks = duration_in_weeks.to_i
end
end
CONTROLLER
------------
class InvoiceController < ApplicationController
def index
@enquiry = Enquiry.new
@courses = Course.active_courses
@courses = Course.active_courses
render :layout => "welcome"
end
def price
# if params[:id]
# if params[:course[:id]]
if params[:course][:id].empty?
flash[:notice] = ''You must select a course.''
redirect_to :action => "index"
else
if params[:course][:course_duration].empty?
flash[:notice] = ''You must select a duration.''
redirect_to :action => "index"
else
@time = Time.now
# @selected_course = Course.find_by_id(params[:course][:id])
@duration = params[:course][:course_duration]
# @total_price = (@duration.to_i *
@selected_course.price_per_week) + @selected_course.registration_fee
# @course = Course.find(params[:id])
# @selected_course.discounted_price
@course = Course.find(params[:course][:id])
@course.duration_in_weeks = params[:course][:duration]
# @course.discounted_price <- not needed here.
render :layout => "welcome"
end
end
end
end
-----------
INPUT PAGE
<% form_tag :action => ''price'' do %>
Select a course in the box below<br>
<%= collection_select(:course, :id, @courses, :id, :name, options
={:prompt => "-Select a course"}, :class =>"course")
%><br>
Select a duration (weeks) in the box below<br>
<%= select(''course'', ''course_duration'',
1..52, { :include_blank => true
} ) %><br>
<%= submit_tag "Proceed to quotation" %>
<% end %>
------------
OUTPUT PAGE
<hr>
<h1><%= @course.name %></h1>
<hr>
<br>
<%= @time.to_formatted_s(:long_ordinal) %><br>
<br>
Price per week: <%= number_to_currency(@course.price_per_week, :unit =>
"£", :separator => ".", :delimiter =>
",") %><br>
Duration: <%= @course.duration_in_weeks %> weeks<br>
Registration Fee: <%= number_to_currency(@course.registration_fee, :unit
=> "£", :separator => ".", :delimiter =>
",") %>
<br>
<br>
<b>
<p>
Total Price: <%= number_to_currency(@course.total_price, :unit =>
"£", :separator => ".", :delimiter =>
",") %><br>
Discounted Price: <%= number_to_currency(@course.discounted_price,
:unit => "£", :separator => ".", :delimiter
=> ",") %><br>
</p>
</b>
<br>
Please note that this price is indicative and not a binding quotation.
The price excludes any fees for accommodation, travel and examinations.
Terms and conditions apply.<br>
<br>
<%= button_to "Return to pricing page", :action =>
"index" %>
--
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 post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---