How to use form_for with rjs

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.

you can pass a parameter with the :with option

http://wiki.rubyonrails.org/rails/pages/observe_field+-+Passing+Parameters

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

On Jun 15, 9:42 am, Frederick C. [email protected]
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.