Jan 02 2008

Announcing SMERF - Simple MEta Rails Form

etienne @ 6:18 pm

 

Update 11 January 2008: SMERF web site completed.

Update 9 January 2008: SMERF Released! 

Update 8 January 2008: I’ve completed the documentation and testing. I’ve created a project on RubyForge which is waiting approval. If this is not ready by tomorrow I will make an alternate link available for download, so if all goes well I should have a download available tomorrow.

I am putting the final touches to a new plugin called SMERF which stands for Simple MEta Rails Form. The plugin is a result of me extracting the code I used for one of my projects which I discussed in an earlier article Ruby on Rails Survey Builder.

I’ve spend the past few weeks rewriting parts of it to make it more generic so that it can be used for all sorts of things including survey’s, questionnaires, data collection forms and so on. Some of the important changes I’ve made include

  • Allow question validations to be written using Ruby code. You can now create your own validation methods as required, there are also a couple of standard system validation methods that come with the plugin. You can specify any number of validation methods for a question by comma separating each method name, e.g. validation: validate_mandatory_question, validate_date_format
  • User responses are now stored in a DB table allowing you to perform analysis on the question responses using SQL.

ToDo V0.2

  • Currently there are no tests
  • Allow forms to be broken into smaller forms, currently you get all questions on a single form
  • Allow the save of user responses as you move from form to form

I’m currently writing a plugin generator that will make installation of the plugin easier, hopefully I’ll have this done very soon. I then need to finish the documentation which is mostly done.

I will keep you informed and let you know when the plugin will be available.

 


Aug 19 2007

fieldWithErrors Overrides my CSS Field Classes

etienne @ 6:07 pm

When there are errors on a form, Rails will normally create a <DIV> element with the fieldWithErrors class wrapped around the fields input statement. If you have your own CSS class wrapped around a field this will be overridden by the fieldWithErrors <DIV> which is a pain.

I was searching for a solution when I found this post:

I’d like to say I came up with this, but I actually found it somewhere else.

I have the following inside my environment.rb file:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  msg = instance.error_message
  error_style = "background-color: #f2afaf"
  if html_tag =~ /<(input|textarea|select)[^>]+style=/
    style_attribute = html_tag =~ /style=[’"]/
    html_tag.insert(style_attribute + 7, "#{error_style}; ")
  elsif html_tag =~ /<(input|textarea|select)/
    first_whitespace = html_tag =~ /\s/
    html_tag[first_whitespace] = " style=’#{error_style}’ "
  end
  html_tag
end

I now get the desired effect when an error occurs, in my case it changes the background color of the field red, you can change it to whatever effect you want without it interfering with your field CSS classes…perfect!