new, form_for and automatically-determined values

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.

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). 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

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 :slight_smile:

Hi Dan,

dankelley wrote:

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

But wait!. There's more !!! :slight_smile:

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) 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

(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.