I am using simple_form and want to override readers for model attributes.
Thus following does not work
class C < ActiveRecord::Base
# it has attribute named pack
def pack
"Some value"
end
end
In view there is following code
<%= simple_form_for @c do |f| %>
<%= f.input :pack %>
<% end %>
thus it should show form with input box having value “Some value” but it is blank. Why doesn’t override work with simple_form?
I figured this out, my comment was basically correct,
simple_formleans on Rails form helpers which useread_attributeto get the value from an ActiveRecord object thus reading the value in the database and not using your method. A symptom of coupling persistence/domain/presentation. The way around this is:Or if you want this to be default behaviour you can create your own form builder on top of
simple_forsuch as:And in your form:
** in Rails 3 I don’t think
libis added to the load path by default so you might need to add it and restart you app or put it inapp/models(disclaimer not a good idea, lib is better).