# Form for Object Creation

**URL:** <https://www.ruby-forum.com/t/form-for-object-creation/85366>\
**Category:** Rails\
**Created:** [February 13, 2007, 12:33am UTC](https://www.ruby-forum.com/t/form-for-object-creation/85366 "2007-02-13T00:33:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurin1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/919ad9/32.png) [@laurin1](https://www.ruby-forum.com/u/laurin1)\
**Post date:** [February 13, 2007, 12:33am UTC](https://www.ruby-forum.com/t/form-for-object-creation/85366/1 "2007-02-13T00:33:36Z")

</div>

Can someone explain what is the correct way with Rails 1.2 to use a form  
to  
create an object.

I know start\_form\_tag is changed to form\_tag but what about form\_for?

I am trying to map a field on to field in the object which is created by  
a  
method call,  
so something like this def password=(pass) and so on. However, this  
seems to  
work fine for me  
in some cases but not in others.

Thanks,

Keith

---

<div class="post-metadata">

**Author:** ![laurin1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/919ad9/32.png) [@laurin1](https://www.ruby-forum.com/u/laurin1)\
**Post date:** [February 13, 2007, 8:04am UTC](https://www.ruby-forum.com/t/form-for-object-creation/85366/2 "2007-02-13T08:04:20Z")

</div>

Both take blocks now. When using form\_for, supply the block with a  
symbol denoting the corresponding object, then you don’t have to  
restate it everyplace:

\<% form\_for :people do |f| %\>

name \<%= f.text\_field :name %\>

address \<%= f.text\_field :address %\>

\<% end %\>

In particular, this is great when there are multiple forms on the  
same page. You specify:

\<% form\_for :people %\>

# stuff here

\<% end %\>

\<% form\_for :addresses %\>  
#stuff here  
\<% end %\>

Does this help?

---

<div class="post-metadata">

**Author:** ![laurin1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/919ad9/32.png) [@laurin1](https://www.ruby-forum.com/u/laurin1)\
**Post date:** [February 13, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/form-for-object-creation/85366/3 "2007-02-13T16:52:15Z")

</div>

Ok, I have rewritten my form code to follow this but its not solving my  
core  
problem.

Maybe its just me but rails seems a bit of a mess when it comes to form  
code.

I have one app that runs against 1.1.6. I have a form with the following  
code:

\<%= form.file\_field(“jar\_file”) %\>

In my model I have:

def jar\_file=(jar\_field)  
#stuff  
end

This works fine. Howevern in a new app running on rails 1.2.1 I have the  
following code:  
\<%= f.password\_field(“password”) %\>

with this in my model:

def password=(pass)  
#stuff  
end

This fails with the error:

undefined method `password’ for #User:0x377e560

Is there a ‘correct’ approach for this. Rails seems to allow me to set  
up forms in  
a number of ways but I can find nothing that says this is the right  
way to do it.

Keith
