0

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?

Evgenia Karunus
  • 10,715
  • 5
  • 56
  • 70
  • Does [this](http://stackoverflow.com/a/10465235/326543) help? – Srikanth Venugopalan Mar 24 '15 at 15:11
  • @SrikanthVenugopalan, no, I'm doing it properly. If I check `hi`'s value with `<%= hi %>` in my views, I'll get `'hello'` just as expected. – Evgenia Karunus Mar 24 '15 at 15:15
  • No, the way that it works is that rails supplies a "get" and "set" method for every db field, ie "def hi" and "def hi=(s)". If you override these in your model then it will use the version from your model. What i suspect is that you haven't defined it properly. Can you add the code for the Product class to your question? – Max Williams Mar 24 '15 at 15:16
  • @MaxWilliams, I think it does use my getter, since when I put `<%= hi %>` in my view, it displays `'hello'`. – Evgenia Karunus Mar 24 '15 at 15:25
  • `hi` alone in the view wouldn't call the model method. have you actually just defined a helper method? it's only a helper that you could call standalone in the view like that. – Max Williams Mar 24 '15 at 15:46
  • There's no "hi" method in your Product model, in that repo. https://github.com/lakesare/edit_form/blob/master/app/models/product.rb – Max Williams Mar 24 '15 at 15:47
  • @MaxWilliams, I put <%= hi %> in my view ----- typo again, sorry! I meant `@product.hi`. – Evgenia Karunus Mar 24 '15 at 16:09
  • @MaxWilliams, in that repo hi == name. When edit form is saved, it does call `def hi=(value) ... end` method. I concluded it also calls getter methods from my model when autopopulating the form, I guess it itsn't the case? – Evgenia Karunus Mar 24 '15 at 16:11
  • @MaxWilliams, yes, seems not to be the case according to evgeny petriv's answer. – Evgenia Karunus Mar 24 '15 at 16:22
  • Argh, why don't you copy and paste your code instead of retyping it, then you won't have typos? This sort of thing adds a massive layer of confusion. – Max Williams Mar 24 '15 at 16:28
  • @MaxWilliams, I tried to make it simpler:-( I'll pay more attention to it. – Evgenia Karunus Mar 24 '15 at 16:30
  • Why are you referring to a method called "hi" when your method is called "name"? This is another thing which just adds confusion. You are saying "please look at my repo, where the method names are different to the ones i've asked about". Can't you see that that is really confusing? – Max Williams Mar 24 '15 at 16:30
  • @MaxWilliams, yep, must be, that was a bad choice of names in that test repo. Usually I just keep them as short and different as I can, didn't think of it being easier to grasp if I keep them the same, sorry for confusion. – Evgenia Karunus Mar 24 '15 at 16:35

2 Answers2

2

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

Eugene Petrov
  • 1,578
  • 1
  • 10
  • 22
0

In your view, in place of

  <%= f.text_area :hi %>

Add:

<%= @model_name.hi%>

It works.

raj_on_rails
  • 144
  • 1
  • 11