Skip to content

Instantly share code, notes, and snippets.

@drakmail
Created February 20, 2016 18:34
Show Gist options
  • Save drakmail/ab5373a6da57e96b17eb to your computer and use it in GitHub Desktop.
Save drakmail/ab5373a6da57e96b17eb to your computer and use it in GitHub Desktop.

Revisions

  1. drakmail created this gist Feb 20, 2016.
    8 changes: 8 additions & 0 deletions drink_habit.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    class DrinkHabit < Habit
    # DB structure
    # id:integer
    # title:text
    # food_amount:integer
    # drink_time:datetime
    # type: string
    end
    8 changes: 8 additions & 0 deletions food_habit.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    class FoodHabit < Habit
    # DB structure
    # id:integer
    # title:text
    # food_amount:integer
    # drink_time:datetime
    # type: string
    end
    9 changes: 9 additions & 0 deletions habit.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class Habit < ActiveRecord::Base
    # DB structure
    # id:integer
    # title:text
    # food_amount:integer
    # drink_time:datetime
    # type: string
    belongs_to :user
    end
    16 changes: 16 additions & 0 deletions habits_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    class HabitsController < ApplicationController
    def update
    habit = Habit.find(params[:id])
    if habit.update habits_params
    redirect_to habit_path(habit)
    else
    render 'show'
    end
    end

    protected

    def habits_params
    params.require(:habit).permit(:id, :title, :food_amount, :drink_time)
    end
    end
    10 changes: 10 additions & 0 deletions habits_slash_edit.html.erb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <%= form_for @habit do |f| %>
    <%= f.text_field :title %>
    <%= if @habit.is_a? FoodHabit %> <!-- better to use rendering of partials and helper -->
    <%= f.text_field :food_amount %>
    <% end %>
    <% if @habit.is_a? DrinkHabit %>
    <%= f.text_field :drink_time %>
    <% end %>
    <%= f.submit %>
    <% end %>