I am trying to validate a form that’s not using a database.
The class (validateable.rb) comes from Rails Recipes and I am not sure
how I can get the validation to work with my controller and view.
It’s probably trivial problem but I new to RoR as well as Ruby.
/lib/validateable.rb from rails recipes
module Validateable
[:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
def method_missing(symbol, params)
if(symbol.to_s =~ /(.)_before_type_cast$/)
send($1)
end
end
def self.append_features(base)
super
base.send(:include, ActiveRecord::Validations)
end
end
/app/controllers/app_controller.rb
class AppController < ApplicationController
def submit
if request.post?
ContactMailer.deliver_contact_message(params[:name],
params[:email], params[:message])
flash[:notice] = “Message sent!”
redirect_to :action => ‘contact’
end
end
/app/views/contact_mailer/contact.rhtml
Contact Page
<%= render :partial => 'navigation' %><% if flash[:notice] -%>
<% end -%>
<%= error_messages_for “contact” %>
<%= start_form_tag(:action => ‘submit’) %>
<p>
<label for="contact_name">Name</label> <br />
<%= text_field("contact", "name") %>
</p>
<p>
<label for="contact_email">E-mail</label> <br />
<%= text_field("contact", "email") %>
</p>
<p>
<label for="contact_message">Message</label> <br />
<%= text_area("contact", "message") %>
</p>
<%= submit_tag("Send") %>
<%= end_form_tag %>
/app/models/contact_mailer.rb
class ContactMailer < ActionMailer::Base
def contact_message(cust_name, cust_email, cust_message)
@subject = “Hi”
@body = “(#{cust_message})”
@recipients = cust_email
@from = “[email protected]”
@sent_on = Time.now
end
end
#this is the problem - how do I get this to work with my controller?
/app/models/contact.rb
class Contact
include Validateable
attr_accessor :email
validates_presence_of :email
end
Thank you