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?
You should use
hour.label
,hour.select
etc insidefields_for :hours
:P.S. Ruby processing of db data is bad:
Use db for it: