FunkyForm

The purpose of form objects is to take user-entered data and perform work on it.

Installation

Add to your Gemfile

gem "funky_form"

Examples

app/models/post.rb

class Post < ActiveRecord::Base
end

app/forms/post_form.rb

class PostForm
  include FunkyForm

  model Post

  attribute :id, Integer
  attribute :title, String
  attribute :body, String

  validates :title, :presence => true, :length => {:maximum => 30}
  validates :body, :presence => true, :length => {:within => 10..30}
end

app/controllers/posts_controller.rb

class PostsController < ApplicationController
  def new
    @post_form = PostForm.new
  end

  def create
    @post_form = PostForm.new(params[:post])

    if @post_form.valid?
      Post.create(@post_form.attributes)
      flash[:notice] = "Successfully created"
      redirect_to :posts
    else
      flash[:alert] = "Validation errors"
      render "new"
    end
  end
end

app/views/posts/new.html.erb

<h1>New Post</h1>
<%= form_for @post_form do |f| %>
  <p>
    <%= f.label :title %>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :body %>
    <%= f.text_area :body %>
  </p>

  <p><%= f.submit %></p>
<% end %>