Skip to content

Instantly share code, notes, and snippets.

@telagraphic
Created September 9, 2012 01:32
Show Gist options
  • Save telagraphic/3681928 to your computer and use it in GitHub Desktop.
Save telagraphic/3681928 to your computer and use it in GitHub Desktop.
class Action < ActiveRecord::Base
belongs_to :day
attr_accessible :energy, :mood, :overall
end
class ActionsController < ApplicationController
before_filter :get_day
def get_day
@day = Day.find(params[:day_id])
end
def create
@action = @day.actions.new(params[:action])
if @action.save
redirect_to @day
else
render "day/show"
end
end
end
class Day < ActiveRecord::Base
attr_accessible :sleep, :wake
has_one :action
end
class DaysController < ApplicationController
before_filter :get_day, :only => [:show, :edit, :update, :destroy]
def get_day
@day = Day.find(params[:id])
end
def index
@days = Day.all
end
def show
@action = @day.action.new
end
def new
@day = Day.new
end
def create
@day = Day.new(params[:day])
if @day.save
redirect_to days_path
else
render "new"
end
end
def edit
@day = Day.find(params[:id])
end
def update
if @day.update_attributes(params[:day])
redirect_to days_path
else
render "edit"
end
end
def destroy
@day.destroy
redirect_to days_path
end
end
Life::Application.routes.draw do
resources :days do
resources :actions
end
root :to => "Days#index"
end
<%= link_to "Back", days_path %>
<table id="days-list">
<thead>
<tr>
<th>Date</th>
<th>Wake</th>
<th>Sleep</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= @day.created_at.strftime("%x") %></td>
<td><%= @day.wake %></td>
<td><%= @day.sleep %></td>
<td><%= link_to "Edit", edit_day_path(@day) %></td>
<td><%= link_to "Delete", @day, :confirm => "Sure?", :method => :delete %></td>
</tr>
</tbody>
</table>
<%= render "actions/new_action" %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment