# Form Name or ID

**URL:** <https://www.ruby-forum.com/t/form-name-or-id/176314>\
**Category:** Rails\
**Created:** [September 29, 2009, 12:41am UTC](https://www.ruby-forum.com/t/form-name-or-id/176314 "2009-09-29T00:41:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2009, 12:41am UTC](https://www.ruby-forum.com/t/form-name-or-id/176314/1 "2009-09-29T00:41:00Z")

</div>

Hi all:  
I have a form and I want to use the same code for “new” and “edit”  
methods rendering the view code in a partial.

This view code is working for the “new” method but do not for the  
“edit”, cause of the name of the form object.

Code for the New View  
\<%= render :partial =\> “order\_form” %\>

\<%= link\_to ‘Back’, order\_path %\>

Code for the Edit View (same as new…)  
\<%= render :partial =\> “order\_form” %\>

\<%= link\_to ‘Back’, order\_path %\>

Code for the partial View “\_order\_form”

\<% form\_for(@order) do |f| %\>

\<%= observe\_field ‘order\_field1’, :url =\> {:action =\>  
‘calculate’}, :with =\> “Form.serialize(‘new\_order’)” %\>

\<% end %\>

The observe field only works for the “new” because the “hardcoding”  
parameter name of the form.  
For new object the name of the form is “new\_order”, for editing is  
“edit\_order\_19” (19 is an ID for the “order” object)  
The name of the form is depending of the object the view is editing  
and I need the name for the Observe\_field work properly in all  
records.  
How can I pass the name of the form as a variable to the controller ?

Thanks  
FF

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2009, 12:44am UTC](https://www.ruby-forum.com/t/form-name-or-id/176314/2 "2009-09-29T00:44:30Z")

</div>

FF wrote:  
[…]

> For new object the name of the form is “new\_order”, for editing is  
> “edit\_order\_19” (19 is an ID for the “order” object)  
> The name of the form is depending of the object the view is editing  
> and I need the name for the Observe\_field work properly in all  
> records.  
> How can I pass the name of the form as a variable to the controller ?

You don’t need to. Pass it in :locals when you render the partial.

> Thanks  
> FF

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2009, 12:59am UTC](https://www.ruby-forum.com/t/form-name-or-id/176314/3 "2009-09-29T00:59:10Z")

</div>

I need all the form values for the field in the observe\_form call. …  
… Form.serialize(‘new\_order’)…  
because the controller must to do some calcs and refresh the form with  
the results

Can you explain how I shoud to use :locals variable ?  
Thanks,  
FF

On 28 sep, 19:44, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 29, 2009, 4:16am UTC](https://www.ruby-forum.com/t/form-name-or-id/176314/4 "2009-09-29T04:16:57Z")

</div>

FF wrote:

> I need all the form values for the field in the observe\_form call. …  
> … Form.serialize(‘new\_order’)…  
> because the controller must to do some calcs and refresh the form with  
> the results
> 
> Can you explain how I shoud to use :locals variable ?

Easy. Just put Form.serialized(object\_name) in the partial instead of  
hard-coding. Then you can do :locals =\> {:object\_name =\>  
‘edit\_order\_19’} or whatever. Remember, it’s just a string.

> Thanks,  
> FF
> 
> On 28 sep, 19:44, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
