How to access the object bound to form in partial?

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

On Sun, Aug 3, 2008 at 2:20 AM, Sam K.
[email protected] 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/

Hi Greg,

Greg D. wrote:

On Sun, Aug 3, 2008 at 2:20 AM, Sam K.
[email protected] 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

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

On Sun, Aug 3, 2008 at 2:52 AM, Sam K.
[email protected] 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/

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