On 5/25/07, Nik Wakelin <[EMAIL PROTECTED]> wrote:
> The real problem seems to be that people want to format attributes
> before they display them (credit card, phone number). So, how about we
> allow that. Could be as simple as:

Sometimes (or perhaps generally) form builders and objects are too
closely tied -- surely you do not want to pollute your model with a
lot of user interface stuff. Instead, a level of indirection is
useful, such as a form model:

class ProductForm
 def initialize(object)
   @object = object
 end

 def dutch_price
   ('%.2f' % @object.price).gsub('.', ',')
 end

 def dutch_price=(price)
   @object.price = price.to_s.gsub(',', '.')
 end

 def method_missing(name, *args, &block)
   @object.send(name, *args, &block)
 end
end

Now you can do:

<% form_for :product, @product do |form| %>
 <%= form.text_field :title %>
 <%= form.text_field :dutch_price %>
<% end %>

where @product could be initialized thus:

  @product = ProductForm.new(Product.find(params[:id]))

Alexander.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to