Skip to content

Instantly share code, notes, and snippets.

@bigs
Last active December 18, 2015 04:19
Show Gist options
  • Save bigs/5724495 to your computer and use it in GitHub Desktop.
Save bigs/5724495 to your computer and use it in GitHub Desktop.
form conformity. wouldn't it be cool if forms just fit in?
# even with form helpers, creating forms is a pain in the butt
# for web developers every where. so... tedious...
#
# what if there was a better way?
#
# what if forms could... FIT... IN?
#
# what if forms conformed to the rest of your elegant rails workflow?
# that would be pretty nice is what i am thinking.
#
# ACTION TOUR
class User < ActiveRecord::Base
# include!
include Conform
# our attributes
attr_accessible :id, :firstname, :lastname, :country_id, :active
belongs_to :country
# the default input method is text_field, so we don't have
# to worry about :firstname, :lastname, but :country
# (one of many) and :active (boolean) are special cases
conform_field :country, :type => :select, :options => Country.name_id_pairs
conform_field :active, :type => :check_box
end
# for posterity, here's Country
class Country < ActiveRecord::Base
attr_accessible :id, :name
def self.name_id_pairs
self.all.collect { |c| [c.name, c.id] }
end
end
# TAKE IT TO THE VIEWS
...
<%= conform_for @user, :url => user_path(@user), :include => [:firstname, :lastname, :country, :active] %>
...
# will output a form to set a user's first and last name, country
# and active status. each field is prefaced by a (customizable)
# label and will display relevant error messages.
# FUTURE
# right now (i literally just slapped this together, guys) the template
# for the form is fixed, though it would be trivial to (and i plan to)
# allow for customization of form layouts.
@mrjoelkemp
Copy link

Nice! A more seamless form of scaffolding? Templates in bootstrap with auto-generated js validation and form delta POSTs on submit. I like the idea of an end to end form solution based on the backend model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment