I’ve setup a quoting system that does a quick job of displaying a price
to a user. I works ok but I need to validate that the user has actually
selected a course and a duration, hopefully with a feedback message. At
the moment this all works apart from if a user submits a blank form
(then I understandably get a nill object error)
How do I do this?
Here’s the code so far…
=============
Initial Form
<% form_tag :action => ‘price’ do %>
<%= collection_select(:course, :id, @courses, :id, :name, options
={:prompt => “-Select a course”}, :class =>“course”) %>
<%= select(‘course’, ‘course_duration’, 1…52, { :include_blank => true
} ) %>
<%= submit_tag “Price” %>
<% end %>
Controller
class InvoiceController < ApplicationController
def index
@enquiry = Enquiry.new
@courses = Course.active_courses
@courses = Course.active_courses
render :layout => “welcome”
end
def price
@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
render :layout => "welcome"
end
end
Display the price view
<%= @time %>
Price Below
Course: <%= @selected_course.name %>
Price per week: <%= number_to_currency(@selected_course.price_per_week, :unit => "£", :separator => ".", :delimiter => ",") %>
Duration: <%= @duration %> weeks
Total Price: <%= number_to_currency(@total_price, :unit => “£”,
:separator => “.”, :delimiter => “,”) %>
Please note that this price is indicative and not a binding quotation. Terms and conditions apply.