This project is archived and is in readonly mode.

#6454 new
habermann24

Custom methods' return values won't apply on form helper like text_field!

Reported by habermann24 | February 19th, 2011 @ 05:15 PM

Say there is the following model with a database column 'reference_id' (of type string).


class Post < ActiveRecord::Base

  def reference_id
    if self.new_record?
      return "test"
    else
      return self[:reference_id]
    end
  end

end

Somewhere in the form one would do:


builder.text_field :subject

In Rails 3.0.0 the behaviour was that when i render "new" with a new record assigned, the text_field would get the value "test" (because that is what the method returns).

Now the text_field will be empty. Rails probably just reads the attribute (which is still nil) and doesn't call the method to fill out the value.

I used that technique to provide my own attribute methods for default values etc. Now i always have to provide ":value => @post.reference_id" to the text_field, so that i get the same result.

Is this supposed to be the new behaviour? Or was that an unintentional change / bug?

I hope this example is clear enough!

No comments found

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages