# How to use form\_for with rjs

**URL:** <https://www.ruby-forum.com/t/how-to-use-form-for-with-rjs/140479>\
**Category:** Rails\
**Created:** [June 14, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-use-form-for-with-rjs/140479 "2008-06-14T19:26:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![athem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@athem](https://www.ruby-forum.com/u/athem)\
**Post date:** [June 14, 2008, 7:26pm UTC](https://www.ruby-forum.com/t/how-to-use-form-for-with-rjs/140479/1 "2008-06-14T19:26:08Z")

</div>

Hello,

How can I use a form variable in a partial rendered from within a  
page.replace\_html rjs call? Here’s the problem:

I’m trying to render a partial with form fields using rjs.

In the erb template:

\<% form\_for :availability, @availability, :url =\> { :action =\>  
“post\_listing” } do |f| %\>

\<%= f.select @availability, availabilities, :id =\> “availability\_type”  
%\>

\<%= observe\_field(:availability\_,  
:frequency =\> 0.5,  
:update =\> :availability,  
:url =\> { :controller =\>  
‘listing’, :action =\> :change\_availability\_type}) %\>

\<% end %\>

In the controller action:

def change\_availability\_type  
render :update do |page|  
page.replace\_html “availability”, :partial =\> “dry\_lease”  
end  
end

In the partial \_dry\_lease.rhtml:

\<%= f.text\_field :dry\_lease\_min\_months %\>

The problem is that the variable f in the partial above (i.e.,  
f.text\_field) is not set (it’s nil).

In this part of the appllication, I need to dynamically render  
different partials with form elements depending on the option selected  
by the user.

When not using rjs, you could just pass the form variable to the  
partial using :locals =\> { }. But in rjs, the call to  
page.replace\_html takes place in the controller, it’s not clear how  
the form variable f could be set in the partial.

Thanks.

---

<div class="post-metadata">

**Author:** ![athem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@athem](https://www.ruby-forum.com/u/athem)\
**Post date:** [June 15, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/how-to-use-form-for-with-rjs/140479/2 "2008-06-15T18:31:04Z")

</div>

you can pass a parameter with the :with option

[http://wiki.rubyonrails.org/rails/pages/observe\_field+-+Passing+Parameters](http://wiki.rubyonrails.org/rails/pages/observe_field+-+Passing+Parameters)

---

<div class="post-metadata">

**Author:** ![athem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@athem](https://www.ruby-forum.com/u/athem)\
**Post date:** [June 15, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/how-to-use-form-for-with-rjs/140479/3 "2008-06-15T18:43:27Z")

</div>

On 14 Jun 2008, at 18:25, athem wrote:

> 

You’re going to need to create the form builder yourself. obviously  
you don’t want form\_for since that would create form tags etc…, you  
just want the bit of form\_for that knows how to name form parameters  
and so on, that bit is fields\_for.

Fred

---

<div class="post-metadata">

**Author:** ![athem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@athem](https://www.ruby-forum.com/u/athem)\
**Post date:** [June 17, 2008, 5:59am UTC](https://www.ruby-forum.com/t/how-to-use-form-for-with-rjs/140479/4 "2008-06-17T05:59:46Z")

</div>

On Jun 15, 9:42 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > In the partial \_dry\_lease.rhtml:  
> > The problem is that the variable f in the partial above (i.e.,
> 
> > Thanks.

Based on Fred’s suggestion, the solution is to (1) continue to use  
form\_for to create the form, but (2) enclose the fields in each of the  
partials using fields\_for. Each of the partials is then self-  
contained and can be dropped into the form using RJS  
page.replace\_html. Thanks.
