Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active November 1, 2016 05:25
Show Gist options
  • Select an option

  • Save jendiamond/10259c78b320e61f8f44a660f125f6b6 to your computer and use it in GitHub Desktop.

Select an option

Save jendiamond/10259c78b320e61f8f44a660f125f6b6 to your computer and use it in GitHub Desktop.

app/controllers/baths_controller.rb

class BathsController < ApplicationController
  before_action :set_bath, only: [:show, :edit, :update, :destroy]

  # GET /baths
  def index
    @baths = Bath.all
  end

  # GET /baths/1
  def show
  end

  # GET /baths/new
  def new
    @bath = Bath.new
  end

  # GET /baths/1/edit
  def edit
  end

  # POST /baths
  def create
    @bath = Bath.new(bath_params)

    respond_to do |format|
      if @bath.save
        format.html { redirect_to @bath, notice: 'Bath was successfully created.' }
        format.json { render :show, status: :created, location: @bath }
      else
        format.html { render :new }
        format.json { render json: @bath.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /baths/1
  def update
    respond_to do |format|
      if @bath.update(bath_params)
        format.html { redirect_to @bath, notice: 'Bath was successfully updated.' }
        format.json { render :show, status: :ok, location: @bath }
      else
        format.html { render :edit }
        format.json { render json: @bath.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /baths/1
  def destroy
    @bath.destroy
    respond_to do |format|
      format.html { redirect_to baths_url, notice: 'Bath was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_bath
      @bath = Bath.find(params[:id])
    end

    def bath_params
      params.require(:bath).permit(:day_id, :bath, :notes)
    end
end

app/controllers/days_controller.rb

class DaysController < ApplicationController
  before_action :set_day, only: [:show, :edit, :update, :destroy]

  # GET /days
  def index
    @days = Day.all
  end

  # GET /days/1
  def show
    @bath = Bath.find_by day_id:(@day.id)
    @feeding = Feeding.find_by day_id:(@day.id)
  end

  # GET /days/new
  def new
    @day = Day.new
    @bath = @day.build_bath
  end

  # GET /days/1/edit
  def edit
  end

# POST /days
  def create
    @day = Day.new(day_params)

    respond_to do |format|
      if @day.save
        format.html { redirect_to @day, notice: 'Day was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end 

  # PATCH/PUT /days/1
  def update
    respond_to do |format|
      if @day.update(day_params)
        format.html { redirect_to @day, notice: 'Day was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  # DELETE /days/1
  def destroy
    @day.destroy
    respond_to do |format|
      format.html { redirect_to days_url, notice: 'Day was successfully destroyed.' }
    end
  end

  private

    def set_day
      @day = Day.find(params[:id])
    end

    def day_params
      params.require(:day).permit(:week, :date, :notes,
      :bath_attributes => [:bath,:notes])
    end
end

app/controllers/feedings_controller.rb

class FeedingsController < ApplicationController
  before_action :set_feeding, only: [:show, :edit, :update, :destroy]

  # GET /feedings
  def index
    @feedings = Feeding.all
  end

  # GET /feedings/1
  def show
  end

  # GET /feedings/new
  def new
    @day = Day.find(params[:day_id])
    @feeding = @day.feedings.new
  end

  # GET /feedings/1/edit
  def edit
  end

  # POST /feedings
  def create
    @day = Day.find(params[:day_id])
    @feeding = @day.feedings.create
    respond_to do |format|
      if @feeding.save
        format.html { redirect_to @day, notice: 'feeding was successfully created.' }
      else
        format.html { render :new }
      end
    end
  end

  # PATCH/PUT /feedings/1
  def update
    respond_to do |format|
      if @feeding.update(feeding_params)
        format.html { redirect_to @feeding, notice: 'Feeding was successfully updated.' }
      else
        format.html { render :edit }
      end
    end
  end

  # DELETE /feedings/1
  def destroy
    @feeding.destroy
    respond_to do |format|
      format.html { redirect_to feedings_url, notice: 'Feeding was successfully destroyed.' }
    end
  end

  private
    def set_feeding
      @feeding = Feeding.find(params[:id])
    end

    def feeding_params
      params.require(:feeding).permit(:day_id, :feeding_time, :left, :right, :minutes, :feeding_type, :feeding_amount, :notes)
    end
end

app/models/day.rb

class Day < ApplicationRecord
  has_many :feedings, inverse_of: :day, dependent: :destroy 
  has_many :pumps, inverse_of: :day, dependent: :destroy 
  has_many :diapers, inverse_of: :day, dependent: :destroy 
  has_many :sleeps, inverse_of: :day, dependent: :destroy 
  has_many :tummy_times, inverse_of: :day, dependent: :destroy 
  has_one :bath, dependent: :destroy 
  accepts_nested_attributes_for :feedings
  accepts_nested_attributes_for :pumps
  accepts_nested_attributes_for :diapers
  accepts_nested_attributes_for :sleeps
  accepts_nested_attributes_for :tummy_times
  accepts_nested_attributes_for :bath
end

config/routes.rb

Rails.application.routes.draw do
  root 'days#new'
  resources :days do
    resources :feedings, :except => [:update, :destroy]
    resources :pumps, :except => [:update, :destroy]
    resources :diapers, :except => [:update, :destroy]
    resources :sleeps, :except => [:update, :destroy]
    resources :tummy_times, :except => [:update, :destroy]
    resources :baths, :except => [:update, :destroy]
  end
  resources :feedings, :only => [:update, :destroy]
  resources :pumps, :only => [:update, :destroy]
  resources :diapers, :only => [:update, :destroy]
  resources :sleeps, :only => [:update, :destroy]
  resources :tummy_times, :only => [:update, :destroy]
  resources :baths, :only => [:update, :destroy]
end

db/schema.rb

ActiveRecord::Schema.define(version: 20161028005449) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "baths", force: :cascade do |t|
    t.integer  "day_id"
    t.boolean  "bath"
    t.text     "notes"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["day_id"], name: "index_baths_on_day_id", using: :btree
  end

  create_table "days", force: :cascade do |t|
    t.integer  "week"
    t.datetime "date"
    t.text     "notes"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "feedings", force: :cascade do |t|
    t.integer  "day_id"
    t.datetime "feeding_time"
    t.boolean  "left"
    t.boolean  "right"
    t.integer  "minutes"
    t.string   "feeding_type"
    t.integer  "feeding_amount"
    t.text     "notes"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
    t.index ["day_id"], name: "index_feedings_on_day_id", using: :btree
  end

  add_foreign_key "baths", "days"
  add_foreign_key "feedings", "days"

end
### `app/views/days/_feedings.html.erb`
<%= simple_form_for :feedings do |f| %>
<%= f.input :feeding_time, label: "Feeding Time", as: :datetime %>
<%= f.input :left, label: "Left", as: :boolean %>
<%= f.input :right, label: "Right", as: :boolean %>
<%= f.input :minutes, label: "Minutes of Feeding", as: :integer %>
<%= f.input :feeding_type, collection: [ "B", "BF", "BVF"], as: :radio_buttons %>
<%= f.input :feeding_amount, label: "Feeding Amount in ML.", as: :integer %>
<%= f.input :notes, label: "Feeding Notes", as: :text %>
<% end %>

$rake routes

         Prefix Verb   | URI Pattern                                | Controller#Action

---------------------------|--------------------------------------------|------------------- root GET | / | days#new day_feedings GET | /days/:day_id/feedings(.:format) | feedings#index POST | /days/:day_id/feedings(.:format) | feedings#create new_day_feeding GET | /days/:day_id/feedings/new(.:format) | feedings#new edit_day_feeding GET | /days/:day_id/feedings/:id/edit(.:format) | feedings#edit day_feeding GET | /days/:day_id/feedings/:id(.:format) | feedings#show days GET | /days(.:format) | days#index POST | /days(.:format) | days#create new_day GET | /days/new(.:format) | days#new edit_day GET | /days/:id/edit(.:format) | days#edit day GET | /days/:id(.:format) | days#show PATCH | /days/:id(.:format) | days#update PUT | /days/:id(.:format) | days#update DELETE | /days/:id(.:format) | days#destroy feeding PATCH | /feedings/:id(.:format) | feedings#update PUT | /feedings/:id(.:format) | feedings#update DELETE | /feedings/:id(.:format) | feedings#destroy bath PATCH | /baths/:id(.:format) | baths#update PUT | /baths/:id(.:format) | baths#update DELETE | /baths/:id(.:format) | baths#destroy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment