Search code examples
ruby-on-railsrubyformsauto-populate

Rails autopopulates form with database values thus ignoring my model methods


My view:

<%= form_for @product do |f| %>
  <%= f.text_area :hi %>
<% end %>

My model:

def hi
  'hello'
end

Which works properly from console - no matter what :hi attribute really is in the database, it's retrieved as 'hello'.

I guess Rails ignores model method overrides when autopopulating forms? How do I make it pay attention to my override?


Solution

  • Rails gets contents of the textarea from #attributes[:hi].value_before_typecast, so to get content to be filed from product.hi you should use content_tag