views:

1049

answers:

2

Is there a way to use fields_for with in a form without having a scope?

For example:

<% fields_for "user[]" do |x|
  <%= x.text_field :name %>
<% end %>

Without the user model being loaded in memory?

I got it working using territory[user][][name], but I would like to keep it in ERB.

A: 

is there any specific reason you need to use form_for specifically? Its really designed to be used with an instantiated model object.

Alternatively, why don't you just use the regular form helper tags. You can define it as follows:

<%form_tag :my_form do %>
 <%= text_field_tag :foo, :bar %>
<%end%>

You may want to check the documentation for action view to see how it all works.

Derek P.
The reason I can just use a scope is because the form is not editing existing objects in the database. Its basically selecting a bunch of users from a drop down menu, and on create, each selected user is updated_attribute'd.I believe field_for is locked into a scope from reading the API.
dMix
My next question would be how to pass the hash (team[]) using a regular form helper tag?
dMix
+1  A: 

I think the answer would be 'no', since those form_for and fields_for would try to determine default value from that given instance variable.

However, I think if you want to lower memory usage from loading that model, you might try to create a mock-up model to return nil values, and create a instance object from that one instead.

Sikachu