# Default values for new table entry

**URL:** <https://www.ruby-forum.com/t/default-values-for-new-table-entry/166227>\
**Category:** Rails\
**Created:** [April 23, 2009, 12:38am UTC](https://www.ruby-forum.com/t/default-values-for-new-table-entry/166227 "2009-04-23T00:38:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [April 23, 2009, 12:38am UTC](https://www.ruby-forum.com/t/default-values-for-new-table-entry/166227/1 "2009-04-23T00:38:29Z")

</div>

All,

I would like a new record that is created with the new.html.erb page  
to always populate a specific variable with a default value.

The simplest way would be to have a check\_box that defaults to checked  
when the page is loaded, but this seems to be tricky.

Current code looks something like  
form\_for(@foo) do |f|  
…  
check\_box(:f, :this\_should\_always\_be\_yes\_from\_this\_form, {}, 1, 0)

1. Can I set this value automatically?
2. Can I get the check box to default to “yes”?

Thanks,  
Ben

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [April 23, 2009, 5:10am UTC](https://www.ruby-forum.com/t/default-values-for-new-table-entry/166227/2 "2009-04-23T05:10:14Z")

</div>

You can do this in the controller:

@foo = Foo.new(:parameter\_name =\> true)

And then this in the view:

form\_for(@foo) do |f|

f.check\_box :parameter\_name

Regards.

Franco C…

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [April 27, 2009, 6:22am UTC](https://www.ruby-forum.com/t/default-values-for-new-table-entry/166227/3 "2009-04-27T06:22:10Z")

</div>

this worked great, thanks

–b

---

<div class="post-metadata">

**Author:** ![cbnewman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/0ea827/32.png) [@cbnewman](https://www.ruby-forum.com/u/cbnewman)\
**Post date:** [April 27, 2009, 6:39am UTC](https://www.ruby-forum.com/t/default-values-for-new-table-entry/166227/4 "2009-04-27T06:39:09Z")

</div>

You can do this in the controller, but I’d advise against this.

Why? Business logic is best located and/or managed by the model. The  
easiest way to do this would be to modify the database column to set a  
default value, which Active Record will read when it looks at the  
table structure and use this to set default values.

For example, if you had a migration for a new field, you could do the  
following:

add\_column :teams, :active, :boolean, :default =\> false

If we had a Team model, when we did Team.new, we’d see that there  
would be a default value already set for the active attribute.

As mentioned, this would be the simplest method and would keep the  
business logic located in the realm of the model. Keep controller code  
short and sweet.

Hope this helps!

Cheers,  
Robby

On Wed, Apr 22, 2009 at 8:09 PM, Franco C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > The simplest way would be to have a check\_box that defaults to checked  
> > Thanks,  
> > Ben

–  
Robby R.  
Chief Evangelist, Partner

PLANET ARGON, LLC  
design // development // hosting w/Ruby on Rails

> **[Planet Argon](https://www.planetargon.com/)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[http://robbyonrails.com/](http://robbyonrails.com/)  
[http://twitter.com/planetargon](http://twitter.com/planetargon)  
aim: planetargon

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4068 [fax]
