Passing un-editable model attributes from a form


#1

Dear anyone,

I’m trying to create a very simple “form_for” form which, when
submitted, will create, via a RESTFUL controller, a new model
object…all exactly as per the scaffold_resource-generated
model/controller/view but with one difference:

At the time the form is generated, I compute the appropriate value for 2
of this model’s 3 columns based on instance variables floating around at
that time (they depend on the view the user is being presented with)

The form presents the user with the remaining 1 attribute to fill in.

Upon submission, how do I get my 2 predetermined values to the
controller within the standard params[:model_name] hash? I don’t want to
set my 2 attributes within the controller, as I want the controller
“create” action to remain more generic than that.

I imagine this is trivial…if you know how!

Many thanks, Nathaniel


#2

Look them up when you are generating the html form (if they are in a
DB) and pass them back as hidden fields in the form.

On Mar 21, 2:25 pm, Nathaniel removed_email_address@domain.invalid


#3

Andrew, Matthew,

Thanks to both of you. “form.hidden_field” is exactly what I was
after—I was ignorant to its existence!

3 hours wasted this afternoon; fixed in 3 minutes with your help.

Thanks again

Nathaniel


#4

Nathaniel wrote:

The form presents the user with the remaining 1 attribute to fill in.

Upon submission, how do I get my 2 predetermined values to the
controller within the standard params[:model_name] hash? I don’t want to
set my 2 attributes within the controller, as I want the controller
“create” action to remain more generic than that.

You mean other than just using hidden fields like so:
form.hidden_field(:some_predefined_attr). You just have to fill in the
value for :some_predfined_attr in the object before you run your
form_for on it.

-matthew