but it seems like this shouldn’t be necessary. I also override read
accessors for values that I use in select statements on the same form
and it works fine.
def text_color
read_attribute(:text_color).nil? ? SiteColors::TEXT_DEFAULT :
read_attribute(:text_color)
end
<%= f.select :text_color, SiteColors::COLORS%>
This picks up the default fine
Is this a bug in text_field or am I just doing something stupid?
The string is nil. I even created a new rails project to test this
out.
(Rails 3 / 1.9.2)
rails generate scaffold test value1:string value2:integer
app/models/test.rb
class Test < ActiveRecord::Base
def value1
read_attribute(:value1).nil? ? ‘hello’ : read_attribute(:value1)
end
def value2
read_attribute(:value2).nil? ? 3 : read_attribute(:value2)
end
end
app/views/tests/_form.html.erb
<%= form_for(@test) do |f| %>
value 1 is <%= @test.value1.inspect %> and value 2 is <%= @test.value2.inspect %>
Sorry, I can’t help you out as I’m not on my dev box. One thing off the
top
of my mind (so you wont have to worry about that) is to use a
placeholder
attribute instead of doing that check. Good luck!