You should give a block to fields_for
. For more information on that method see docs. In your case, first, add this line to your User
model:
class User
has_many :leaves
accepts_nested_attributes_for :leaves
end
This is required so that when your form is posted, the attributes coming from the form fields for leaves via params
were handled correctly.
Now your template should look like this (for simplicity by now I assume that your Leave
also has a simple text field named foo
):
<%= form_for @user do |f| %>
...
<%= f.fields_for :leaves do |leave_fields| %>
# Fields for a leave here ----v
Foo: <%= leaves_fields.text_field :foo %>
<% end %>
<% end %>
Or, if you @user.leaves
already initialized and you want form builder to put its values to form fields, you have to iterate over @user.leaves
, passing each of them to fields_for
as second argument:
<%= form_for @user do |f| %>
...
<% @user.leaves.each do |leave| %>
<%= f.fields_for :leaves, leave do |leave_fields| %>
# Note the addition here --^
Foo: <%= leave_fields.text_field :foo %>
<% end %>
<% end %>
<% end %>
But your question has another one inside: you have not a text field, but a hash, and there is no default form input for it (i.e. there is no f.hash_field :leaves_types
). So you may want to make it by yourself like suggested in these questions: [1], [2] and [3].
Anyway, having a Hash field seems rather uncommon to me, so maybe Hash can be somehow replaced, say, with another has_many
association (not sure), and in this case you will only need another nested fields_for
.