Rails: fields_for and collection

I would like to use an extra collection for fields_by_. this collection should contain all the features that will be used in fields_for.

Let's say that I have a person with tasks that will happen regularly every week on the same day. In the form of a person, I should have a record for every day, even if there are no saved tasks. I tried:

<% form_for(@person) do |f| %>
...

  <% f.fields_for :tasks, @weekdays do |task_fields| %> 
    <%= weekday.name %>: 
    <%= project_fields.text_field :name %>
  <% end %>
<% end %>

Now for each day of the week there should be a text field for entering the task name of that day. e.g. weekday.name = "monday" and task.name = "drink coffee", task.weekday_id = 1

+3
source share
1 answer

week_days. :

<% @weekdays.each_with_index do |weekday, i| %>
  <% f.fields_for :tasks do |task_fields| %> 
    <%= weekday.name %>: 
    <%= task_fields.text_field :name %>
    <%= task_fields.hidden_field :weekday_id, :value => (i + 1) %>
  <% end %>
<% end %>

"weekdays", hidden_field weekday.id

: 30

, . .

<% f.fields_for :tasks, @weekdays do |task_fields| %> 
  <%= weekday = task_fields.object %>
  <%= weekday.name %>: 
  <%= task_fields.text_field :name %>
  <%= task_fields.hidden_field :weekday_id, :value => weekday.id %>
<% end %>
0

Source: https://habr.com/ru/post/1779980/


All Articles