Forum: Ruby on Rails default value in text field

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B5a793b425c4db1f45c3cdfec5697047?d=identicon&s=25 ss (Guest)
on 2006-05-12 15:10

This should be really simple, but i cant seem to find the answer

In my _form.rhtml I have the following text field, how can I define a
default value?
<%= text_field 'purchaseorder', 'number'  %>
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-05-12 15:24
Back in your controller action, do:

def new
@purchaseorder.number = "some number"
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2006-05-12 15:29
(Received via mailing list)
<%= text_field 'purchaseorder', 'number', :value => "some value" %>

but like Bryan said, if you have an instance variable named
purchaseorder, rails should automatically fill in the default value
with the attributes of this variable.

B5a793b425c4db1f45c3cdfec5697047?d=identicon&s=25 ss (Guest)
on 2006-05-12 15:33
Bryan Duxbury wrote:
> Back in your controller action, do:
> def new
> @purchaseorder.number = "some number"
> end

thanks :)
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-05-13 05:05
(Received via mailing list)
Possibly a better way to accomplish this is to set a default on the
model object when it is instantiated:

class PurchaseOrder < ActiveRecord::Base
  def initialize(attributes = nil)
    self.number = 123 unless number?

This topic is locked and can not be replied to.