# Validations (when not saving to the database)

**URL:** <https://www.ruby-forum.com/t/validations-when-not-saving-to-the-database/146719>\
**Category:** Rails\
**Created:** [August 24, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/validations-when-not-saving-to-the-database/146719 "2008-08-24T18:12:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 24, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/validations-when-not-saving-to-the-database/146719/1 "2008-08-24T18:12:09Z")

</div>

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.
