-
Notifications
You must be signed in to change notification settings - Fork 183
Hstore Attributes
Ahmad Musaffa edited this page Aug 6, 2014
·
2 revisions
If you want PostgreSQL's Hstore attributes to be validated by Reform's form object, you can follow these example settings.
# app/models/profile.rb
class Profile < ActiveRecord::Base
# name is a normal column
# info is a hstore column
store_accessor :info, :occupation, :academic_subject, :educational_institutions, :address
# rest of the codes ....
end
## app/forms/profile_form.rb
class ProfileForm < Reform::Form
properties [:name, :occupation, :academic_subject, :educational_institutions, :address]
validates :name, presence: true, length: { within: 3..50 }
validates :occupation, length: { maximum: 50 }
validates :academic_subject, length: { maximum: 50 }
validates :educational_institutions, length: { maximum: 50 }
validates :address, length: { maximum: 50 }
end
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def process_form(form, params)
if form.validate(params)
form.save do |data, map|
yield data, map
end
else
render json: form.errors, status: :unprocessable_entity
end
end
end
# app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
respond_to :html, :js
def new
@form = ProfileForm.new(Profile.new)
end
def create
@form = ProfileForm.new(Profile.new)
process_form(@form, params[:profile]) do |data, map|
profile = current_user.create_profile(map)
profile.save
respond_with profile
end
end
end
# app/views/profiles/new.html.haml
= form_for @form, data: { remote: true } do |f|
= f.text_field :name
= f.text_field :occupation
= f.text_field :academic_subject
= f.text_field :educational_institutions
= f.text_field :address
= f.submit