fuel_consumption.rbRails 5, как создавать вложенные атрибуты следующим
class FuelConsumption < ApplicationRecord
after_initialize :set_defaults, unless: :persisted?
def set_defaults
self.date ||= Date.today
end
belongs_to :boiler
validates :fuel, numericality: {message: 'Поле может быть только числовое'}
validates :fuel, presence: 'Поле топлива не может быть пустым'
validates_uniqueness_of :date, scope: :boiler_id, message: 'Эта дата уже заполнена'
validates :date, presence: 'Дата не может быть пустой'
end
new.html.slim
h4 Расход топлива
= form_for(@branch, url: {action: 'create'}, method: 'post') do |f|
= f.fields_for :boilers do |ff|
= ff.fields_for :fuel_consumption do |fff|
.form-group
= fff.label :fuel, 'Дата'
= fff.text_field :date, class: 'form-control', disabled: 'disabled', value: I18n.l(Date.today, format: :long)
.form-group
= fff.label :fuel, 'Количество топлива'
= fff.text_field :fuel, class: 'form-control'
.form-group
= f.submit 'Сохранить', class: "btn btn-lg btn-success"
fuel_consumption_controller.rb
def new
@branch = current_user.branches.first
# @fuel_consumption = FuelConsumption.new
end
def create
@branch = current_user.branch.update_attributes(branch_params)
if @branch
render :success
else
flash.alert = 'Oops'
render :action => 'new'
end
end
def branch_params
params.require(:branch).permit!
end
Я назначил ветвь пользователя 1, у которой мало (4 или 5) котлов, и я хочу создать расход топлива на каждом котле с одной страницы, как этого достичь. Этот код обновляет атрибуты, но не создает fuel_consumption.