I can get hstore to work with simple_form but all but the most basic functionality (saving) just doesn't work. Validation messages don't show up on the individual fields... all hstore fields oddly show as required, even the values themselves don't populate correctly unless set manually.
I have to do something like this:
<%= f.simple_fields_for :phones do |phone| %>
<%= phone.input :agent, :input_html => { :value => @artist.phones['agent'] } %>
<% end %>
I have to use simple_fields_for for the hstore hash and it saves properly but on edit the values don't populate without using the input_html to set the value. It marks every field as required and validation errors don't show up at all (they do work).
Using hstore validations like so (added from below answer):
validates_hstore :emails do
validates_format_of [:agent,:artist], :with => /@/, :allow_blank => true
end
Any ideas? Thanks.
You can find an example of how to add some custom validations for Hstore here:
https://gist.github.com/rf-/2322543
module HstoreValidation
def validates_hstore(field, &block)
validation_class = Class.new do
include ActiveModel::Validations
def self.name
'(validations)'
end
def initialize(data)
@data = data
end
def read_attribute_for_validation(attr_name)
@data[attr_name]
end
end
validation_class.class_eval &block
validate do
validator = validation_class.new(self[field])
if validator.invalid?
validator.errors.each do |attr, text|
self.errors.add(attr, text)
end
end
end
end
end
But as for how to get the validations to work with Simple form, I'm not sure.
来源:https://stackoverflow.com/questions/17523112/simple-form-and-hstore-basic-functionality