On Nov 15, 2012, at 1:15 AM, Soichi Ishida wrote:
> 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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to
rubyonrails-ta...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>