# New, form\_for and automatically-determined values

**URL:** <https://www.ruby-forum.com/t/new-form-for-and-automatically-determined-values/82826>\
**Category:** Rails\
**Created:** [January 21, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/new-form-for-and-automatically-determined-values/82826 "2007-01-21T16:13:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [January 21, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/new-form-for-and-automatically-determined-values/82826/1 "2007-01-21T16:13:11Z")

</div>

I have something I’m calling “tag”, and it has a text field plus (among  
other things) a field to hold the time of creation. My goal is to  
present the user with a form asking for the text field, but to infer  
(and save) the timestamp automatically.

Should I be doing something in my “view”, or somewhere else. And,  
well, what is it that I’ll be doing there?

PS. I’m new to RoR, and I hope my subject line is reasonable, and that  
this posting isn’t a bother to those who are used to working at a  
higher level.

Dan.

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [January 21, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/new-form-for-and-automatically-determined-values/82826/2 "2007-01-21T17:15:17Z")

</div>

Hi Dan,

dankelley wrote:

> I have something I’m calling “tag”,

Depending on how you use it, this could cause you trouble. ‘tag’  
already  
has meaning within Rails, especially within the context of forms. You  
might  
want to use a different name, just to save yourself some hard learning.

> and it has a text field plus (among other things)  
> a field to hold the time of creation. My goal is to  
> present the user with a form asking for the text field,  
> but to infer (and save) the timestamp automatically.

Check out ‘created\_at’  
([http://wiki.rubyonrails.org/rails/pages/MagicFieldNames](http://wiki.rubyonrails.org/rails/pages/MagicFieldNames)). Does exactly  
what you’re looking for.

> PS. I’m new to RoR,

Welcome!

> and I hope my subject line is reasonable, and that  
> this posting isn’t a bother to those who are used to  
> working at a higher level.

There are folks here at all levels. That’s one of the things that keeps  
the  
community thriving. Don’t worry about ‘bothering’ folks.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [January 21, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/new-form-for-and-automatically-determined-values/82826/3 "2007-01-21T18:19:25Z")

</div>

Bill’s advice is terrific, and it solves most of my problem. However,  
I probably chose a bad example by using something for which there is  
magic.

Suppose that additionally I want to save something else in the db entry  
that arises after the webform has been submitted. (Example: my site  
will have logged-in users, and I’d like to save is the “id” of the user  
who has used the webform.) Is there a way I can tell RoR to run some  
code, after the form has been submitted?

PS. I realize that I’m probably looking at this problem in an old  
(non-MVC) way, set up by decades of programming within systems that  
have less “magic” and that thus require more coding. But I’m pursuing  
this on this thread in hopes that it helps others who are in the same  
boat … although they may have started with PHP and not Fortran 🙂

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [January 21, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/new-form-for-and-automatically-determined-values/82826/4 "2007-01-21T20:20:41Z")

</div>

Hi Dan,

dankelley wrote:

> I probably chose a bad example by using something  
> for which there is magic.

But wait!. There’s more !!! 🙂

> Suppose that additionally I want to save something else  
> in the db entry that arises after the webform has been  
> submitted. (Example: my site will have logged-in users,  
> and I’d like to save is the “id” of the user who has used  
> the webform.) Is there a way I can tell RoR to run some  
> code, after the form has been submitted?

Several. Easiest to start with is filters.

From the docs (at [http://api.rubyonrails.org](http://api.rubyonrails.org)) on the  
ActionController::Filters::ClassMethods module…

“Filters enable controllers to run shared pre and post processing code  
for  
its actions. These filters can be used to do authentication, caching, or  
auditing before the intended action is performed. Or to do localization  
or  
output compression after the action has been performed. Filters have  
access  
to the request, response, and all the instance variables set by other  
filters in the chain or by the action (in the case of after filters).”

If you’re new to the api documentation you’ll find the module doc links  
in  
the middle-left frame. The lower-left contains links by method into the  
module docs. Poking around in the methods is time well spent.

Don’t worry if the beginning sections are a bit over your head re: OO  
concepts and terminology. Skip to the ‘Filter chain ordering’ section.  
You’re interested in the before\_filter and after\_filter methods. Google  
‘rails after\_filter’ for more, including sample code.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![dankelley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@dankelley](https://www.ruby-forum.com/u/dankelley)\
**Post date:** [January 23, 2007, 1:29am UTC](https://www.ruby-forum.com/t/new-form-for-and-automatically-determined-values/82826/5 "2007-01-23T01:29:28Z")

</div>

(replying to self, with solution that worked for me…)

I ended up doing something simple – I just tacked the code I wanted  
into the “create” method of my controller. This might come back to  
bite me later, but so far it seems a reasonable solution.

Thanks, Bill, for the several threads of advice you’ve given.

Dan.
