3.1 Inputs with attributes using composed_of

ilyakatz edited this page Aug 7, 2011 · 1 revision

If model uses composed_of to convert an attribute to manipulate the value of the attribute symantic_form_for may not present the value of the field properly.

Here is an example of how to add money money (using money gem)

# encoding: utf-8

module Formtastic
  class SemanticFormBuilder

    def money(method, options = {})
      value = object.send(method).format(:no_cents_if_whole => true, :symbol=>false)
      new_input_html = options[:input_html] ? {:value=>value}.merge(options[:input_html]) : {:value=>value}
      options[:input_html]=new_input_html
      input(method, options)
    end
  end

end

After that, you can use form.money as any other input

<%= semantic_form_for @new_rate || Rate.new() do |form| %>
    <%= form.money :value %>
<% end %>