Search code examples
formsruby-on-rails-4high-voltage

Rails: Form in static pages of high voltage gem


I have created few static pages with awesome high_voltage gem.

My pages are static, except a different form in each page.

Forms are supposed to grab user details and send emails (and may be save details in db)

Question:

How should I proceed with this ?

I am concerned more about rails approach,

e.g.

  1. How can I add validation without custom coding (both at client / server side)
  2. How can take help from rails helper method ?

Suppose, I would like to save those fields in db, how should I deal with that ?

In nutshell, I want to use as maximum of rails magic without manually dealing with things.

What I have tried ?

Currently, I have forms in each page. Each form posts at same controller (PagesController) with post method.

I then differentiate them, based on hidden input on respective form.

I do not have any validations for now.


Solution

  • I finally created a model without activerecord, following this great article.

    I created a model like below.

    class Message
    
      include ActiveModel::Validations
      include ActiveModel::Conversion
      extend ActiveModel::Naming
    
      attr_accessor :name, :email, :subject, :body
    
      validates :name, :email, :subject, :body, :presence => true
      validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true
    
      def initialize(attributes = {})
        attributes.each do |name, value|
          send("#{name}=", value)
        end
      end
    
      def persisted?
        false
      end
    
    end