Giter Site home page Giter Site logo

simple_form_object's Introduction

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

simple_form_object's People

Contributors

lengarvey avatar afeld avatar mikel avatar u1tnk avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.