# How to access the object bound to form in partial?

**URL:** <https://www.ruby-forum.com/t/how-to-access-the-object-bound-to-form-in-partial/144853>\
**Category:** Rails\
**Created:** [August 3, 2008, 9:20am UTC](https://www.ruby-forum.com/t/how-to-access-the-object-bound-to-form-in-partial/144853 "2008-08-03T09:20:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 3, 2008, 9:20am UTC](https://www.ruby-forum.com/t/how-to-access-the-object-bound-to-form-in-partial/144853/1 "2008-08-03T09:20:29Z")

</div>

Hi,

In a partial, how do I access the ActiveRecord object that’s bound to a  
form?

\<%= f.text\_field :name %\>  
\<% if f.(something).new\_record? %\>  
…  
\<% end %\>

I want to know if the object is new record or not.  
I can pass the object itself using :locals, but I want to make it DRY.

Thanks.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 3, 2008, 9:36am UTC](https://www.ruby-forum.com/t/how-to-access-the-object-bound-to-form-in-partial/144853/2 "2008-08-03T09:36:28Z")

</div>

On Sun, Aug 3, 2008 at 2:20 AM, Sam K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In a partial, how do I access the ActiveRecord object that’s bound to a  
> form?
> 
> \<%= f.text\_field :name %\>  
> \<% if f.(something).new\_record? %\>

Use if @foo.new\_record?

f.something won’t respond to new\_record?

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 3, 2008, 9:52am UTC](https://www.ruby-forum.com/t/how-to-access-the-object-bound-to-form-in-partial/144853/3 "2008-08-03T09:52:31Z")

</div>

Hi Greg,

Greg D. wrote:

> On Sun, Aug 3, 2008 at 2:20 AM, Sam K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > In a partial, how do I access the ActiveRecord object that’s bound to a  
> > form?
> > 
> > \<%= f.text\_field :name %\>  
> > \<% if f.(something).new\_record? %\>
> 
> Use if @foo.new\_record?

Yes, I know that.  
However, I am not sure if I can assume that the partial knows @foo.  
As partial views are supposed to be shared among controllers, I want to  
minimize the coupling of partial and controller.

I am not sure, though.

Thanks.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 3, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/how-to-access-the-object-bound-to-form-in-partial/144853/4 "2008-08-03T13:07:44Z")

</div>

On 3 Aug 2008, at 08:20, Sam K. wrote:

> \<% end %\>

> I want to know if the object is new record or not.

f.object if my memory serves me correctly.

Fred

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 3, 2008, 9:59am UTC](https://www.ruby-forum.com/t/how-to-access-the-object-bound-to-form-in-partial/144853/5 "2008-08-03T09:59:52Z")

</div>

On Sun, Aug 3, 2008 at 2:52 AM, Sam K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Use if @foo.new\_record?
> 
> Yes, I know that.  
> However, I am not sure if I can assume that the partial knows @foo.

Then don’t assume, test for it.

if @foo && @foo.new\_record?

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [August 4, 2008, 4:58am UTC](https://www.ruby-forum.com/t/how-to-access-the-object-bound-to-form-in-partial/144853/6 "2008-08-04T04:58:17Z")

</div>

Hi Frederick,

Frederick C. wrote:

> On 3 Aug 2008, at 08:20, Sam K. wrote:
> 
> > \<% end %\>
> 
> > I want to know if the object is new record or not.
> 
> f.object if my memory serves me correctly.

That’s right.  
The problem is that if I pass a string to the form\_for, f.object is nil.  
It’s valid only when I pass an object.  
Probably I can use it with f.object\_name.

Thanks.

Sam
