What's wrong with my nested attributes?

69 views Asked by At

I try to add many hours in weekplan, so I code: #app/models/week_plan.rb class WeekPlan < ApplicationRecord has_many :hours accepts_nested_attributes_for :hours end

# app/model/hour.rb
class Hour < ApplicationRecord
  belongs_to :weekplan
end

# app/controllres/weekplan_controller.rb
class WeekplansController < ApplicationController

  def new
    @weekplan = WeekPlan.new
    7.times { @weekplan.hours.build }
  end

  private
    def set_weekplan
      @weekplan = WeekPlan.find(params[:id])
    end

    def weekplan_params
      params.require(:weekplan).permit(hours_attributes: [ :date_start ])
    end
end

And finnaly, app/views/weekplans/new.html.rb

<h1>New Plan</h1>

<%= form_for @weekplan do |f| %>
  <%= f.fields_for :hours do |hour| %>
    <p>
        <%= f.label :user_id %><br>
        <%= f.select :user_id, User.all.collect { |p| [ p.name, p.id ] }, include_blank: true %>
    </p>

    <p>
        <%= f.label :date %><br>
        <%= f.datetime_field :date %>
    </p>

    <p>
        <%= f.label :date_start %><br>
        <%= f.datetime_field :date_start %>
    </p>

    <p>
        <%= f.label :date_end %><br>
        <%= f.datetime_field :date_end %>
    </p>
  <% end %>
  <%= f.submit %>
<% end %>

But, when I run my app, I get the error:

(undefined method `user_id' for #<WeekPlan id: nil, created_at: nil, updated_at: nil>):

I can't understand, where a bug hides?

1

There are 1 answers

1
Andrey Deineko On BEST ANSWER

You should use hour.label, hour.select etc inside fields_for :hours:

f.fields_for :hours do |hour| %>       # <======= hour was not used, use it
    <p>
        <%= hour.label :user_id %><br> # <======= hour, not `f`
        <%= hour.select :user_id, User.all.collect { |p| [ p.name, p.id ] }, include_blank: true %>
        # same with all other nested object's fields

P.S. Ruby processing of db data is bad:

User.all.collect { |p| [ p.name, p.id ]

Use db for it:

User.pluck(:name, :id)