class BedsController < ApplicationController before_action :set_bed, only: %i[ edit update ] before_action :get_crops, only: %i[ index edit update ] def index @beds = Bed.all end def edit end def update if @bed.update(bed_params) redirect_to beds_path, notice: "Bed #{@bed.id} successfully updated." else render :edit, status: :unprocessable_entity end end def bulk_assign_crops crop = Crop.find(params[:crop_id]) Raft.update_all(crop_id: crop.id) redirect_back fallback_location: root_path, notice: "All rafts set to #{crop.name}." end def reset_seed_crops # mirrors seed logic tomatoes = Crop.find_by!(name: "tomatoes") hot_peppers = Crop.find_by!(name: "hot peppers") chives = Crop.find_by!(name: "chives") italian_basil = Crop.find_by!(name: "italian basil") cabbage_chinese = Crop.find_by!(name: "cabbage, chinese") lettuce = Crop.find_by!(name: "lettuce") Bed.includes(:rafts).find_each do |bed| default_crop = case bed.location when 1..2 then tomatoes when 3 then hot_peppers when 4 then chives when 5 then italian_basil when 6..7 then cabbage_chinese else lettuce end bed.rafts.update_all(crop_id: default_crop.id) end redirect_back fallback_location: root_path, notice: "Raft crops reset to default seed layout." end private def set_bed @bed = Bed.find(params[:id]) end def get_crops @crops = Crop.order(:name) end def bed_params params.require(:bed).permit( :location, rafts_attributes: %i[id crop_id] ) end end