The purpose of form objects is to take user-entered data and perform work on it.
Add to your Gemfile
gem "funky_form"
class Post < ActiveRecord::Base end
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
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
<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 %>