Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#6454

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!

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

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

No comments yet.