0

I'm trying to access a Hash type of mongoid in fieds_for and I already have a relationship with a model and want to access a hash of that model. Something like:

class Leave
  field :leaves_types, :type => Hash
end

class User
  has_many :leaves
end

Want to do something like:

form_for @user do |f|
  f.fields_for :leaves.leave_types...

How I can achieve this? Thanks in advance.

partlov
  • 13,789
  • 6
  • 63
  • 82
Nits
  • 3
  • 2
  • Hey! Thank you very much. Actually, first time I'm asking question here and the ask a question view is quite inaccessible for screen readers so, some how manage to ask question and forgot formatting. – Nits Jan 29 '13 at 12:07

1 Answers1

0

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.

Community
  • 1
  • 1
NIA
  • 2,523
  • 22
  • 32
  • Hi NIA, Thanks for reply. I think I need to manage it from controller it self after submitting the form. Cause, I want to store values in the hash as, leaveType id and its value. – Nits Jan 29 '13 at 12:35
  • Indeed, in this case you can avoid `Hash` by adding new model class `LeaveType` with fields `id`, `leave_id` and `value`, and add `has_many :leave_types` to `Leave`. In this case `leave_types` will return an array for which you can iterate normally and again use `fields_for`. If this still doesn't fit you well, that's maybe because I don't completely understand what "leave" and "leave type" actually mean in your business logic. – NIA Jan 29 '13 at 14:20