class ProductsController < ApplicationController # Make an all and new object for every action before_filter :load # This page has no main page so far def index @products = Product.all end def new @product = Product.new 3.times {@product.details.build} @detail = Detail.new 3.times {@detail.properties.build} end # Create a new product def create @product = Product.new(safe_params) if @product.save respond_to do |format| format.html { redirect_to root_path, notice: "Created #{@product.name}."} format.js {} end else redirect_to new_product_path end end # Edit a product.. def edit @product = Product.find(params[:id]) end # Update a product def update @product = Product.find(params[:id]) if @product.update_attributes(safe_params) flash[:notice] = "#{@product.name} updated." @products = Product.all end end # Delete product def destroy @product = Product.find(params[:id]) @product.destroy flash[:notice] = "#{@product.name} deleted." @products = Product.all end # Show products and details def show @product = Product.find(params[:id]) @details = @product.details @properties = @details.properties # @details = Detail.find_all_by_product_id(params[:id]) # @detail = Detail.find_by_product_id(params[:id]) end private # Load all products def load @products = Product.all @product = Product.new end # Permit safe params def safe_params params.require(:product).permit(:product, :name, details_attributes: [:name, :required_at], properties_attributes: [:name] ) end end