No commit activity in last 3 years
No release in over 3 years
Very simple form objects for simple form and rails
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
>= 0

Runtime

 Project Readme

SimpleFormObject

Allows you to make really simple non-persisted form objects or models.

Only suitable for Rails 4 applications.

You don't need to remember to:

  1. include ActiveModel::Model
  2. Set the class model_name so Rails url generation works for forms.

It gives you:

  1. Default values for your form attributes.
  2. Integration with simple_form so you don't need to specify the field type on the form.
  3. Thanks to ActiveModel::Model you can use standard Rails validations on your attributes.

Installation

Add this line to your application's Gemfile:

gem 'simple_form_object'

And then execute:

$ bundle

Usage

Create a form class. I like to put them inside app/forms/. If you prefer to create models then that will work too.

class PostForm
  include SimpleFormObject

  attribute :body, :text
  attribute :title, :string
  attribute :publish_date, :datetime, default: Time.now

  validates_presence_of :body
  validates_presence_of :title
end

SimpleFormObject includes ActiveModel::Model so you don't need to. It also intelligently sets the model_name on the class so that Rails routing works as expected. As an example:

<%= simple_form_for @post_form do |f| %>
  <%= f.input :title        # renders a simple string input %>
  <%= f.input :body         # renders a textarea %>
  <%= f.input :publish_date # renders a datetime select html element %>
<% end %>

Will create a HTML form which will POST to posts_path. In your controller, you can do something like this:

def new
  @post_form = PostForm.new
end

def create
  @post_form = PostForm.new(param[:post])
  if @post_form.valid?
    # save the data
    flash[:notice] = "Post created successfully."
    # redirect somewhere
  else
    flash[:error] = @post_form.errors.full_messages
    render 'new'
  end
end

Todo:

  1. Automatically add good validations for types.
  2. It's tested in spec but better tests wouldn't hurt.

Contributing

  1. Fork it ( http://github.com/reInteractive-open/simple_form_objects/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request