can split form_for into two parts?

Rails 3.1.3

Is it possible to split form_for block into two parts?

More specifically, I want to create and save a model after filling
required fields.
But some of the fields are not required and unnecessary for the model.

<%= form_for(@foo) do |f| %>
  f.field A
<% end %>

and form_tag updates the following select fields in form_for block with
JavaScript

<%= form_tag do %>
...
<% end %>

<%= form_for(@foo) do |f| %>
  f.field B
<% end %>

Apparently if I hit the save button, it needs to save both field A and
B.

Thanks in advance.

soichi

Rails 3.1.3

Is it possible to split form_for block into two parts?

More specifically, I want to create and save a model after filling
required fields.
But some of the fields are not required and unnecessary for the model.

<%= form_for(@foo) do |f| %>
f.field A
<% end %>

and form_tag updates the following select fields in form_for block with
JavaScript

<%= form_tag do %>
...
<% end %>

<%= form_for(@foo) do |f| %>
f.field B
<% end %>

Apparently if I hit the save button, it needs to save both field A and
B.

Are you sure they need to be two separate forms? If your view pseudocode is accurate, these would both post to the same controller method. You can simply hide the non-essential fields with JavaScript, and show them conditionally. If the field is submitted empty, that would not change your model, right? Likewise if there's a default value, that would also be present in the view, just not shown to the visitor.

<button id="show_options">Show Options</button>

f.field A

f.field B, :class => 'optional'

<script type="text/javascript">
var hidden_fields = $$('.optional').invoke('hide');
$('show_options').observe('click',
  function(evt){
    hidden_fields.invoke('toggle');
  }
);
</script>

Walter

<button id="show_options">Show Options</button>

f.field A

f.field B, :class => 'optional'

<script type="text/javascript">
var hidden_fields = $$('.optional').invoke('hide');
$('show_options').observe('click',
  function(evt){
    hidden_fields.invoke('toggle');
  }
);
</script>

Walter

Oh, thanks. Simpler than I thought.

soichi

Is there anyway to manipulate form_tag from controllers?

Like, in order to edit, _form.html.erb must to be filled with the values
for the model.
At the same time, there are some checkbox or radio buttons related to
the form, but they are not needed for the model directly.

I want to choose the values and fill those form_tag fields as well when
users click edit action.

soichi

You can put data in @variables in the controller and then access those
in the view to put whatever you like in the fields.

Colin

I think what you are looking for is fields_for. This let’s you put data in a different part the params hash. This allows you to either get those fields out of the fields for a given model or allows you to update more than one model or a nest model cleanly.

While the Javascript example is clever, I would caution against as it breaks your MVC design pattern putting logic in your view. Doing inline programming as a matter of course will leave the application difficult to maintain over time.

For this case, the Javascript does not sound necessary.