Rails: Fields_for without scope?

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

For example:

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

      

Without loading a custom model into memory?

I was working using the [user] [] [name] territory, but I would like to store it in the ERB.

0


source to share


2 answers


I think the answer is "no", because they form_for

and fields_for

will try to determine the default value of this instance variable.



However, I think that if you want to reduce memory usage when loading this model, you can try creating a mock model to return nil values ​​and instantiate the object instead.

+1


source


Is there any specific reason why you need to use form_for? It is really meant to be used with an instance of an object object.

Alternatively, why don't you just use regular form helper tags. You can define it like this:



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

      

You can check the documentation for a look at the steps to see how it all works.

0


source







All Articles