Skip to content

Instantly share code, notes, and snippets.

@telagraphic
Created September 9, 2012 15:19
Show Gist options
  • Save telagraphic/3684999 to your computer and use it in GitHub Desktop.
Save telagraphic/3684999 to your computer and use it in GitHub Desktop.
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
@habit = @day.habit
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
No route matches {:action=>"show", :controller=>"habits", :day_id=>#<Day id: 4, wake: "7:30 am", sleep: "", created_at: "2012-09-09 20:11:48", updated_at: "2012-09-09 20:11:48">, :id=>nil}
No route matches {:action=>"show", :controller=>"habits", :day_id=>#<Day id: 4, wake: "7:30 am", sleep: "", created_at: "2012-09-09 20:11:48", updated_at: "2012-09-09 20:11:48">, :id=>nil}
class HabitsController < ApplicationController
before_filter :get_day
def get_day
@day = Day.find(params[:day_id])
end
def new
@habit = @day.build_habit
end
def create
@habit = @day.build_habit(params[:habit])
if @habit.save
redirect_to @day
else
render "day/show"
end
end
def edit
@habit = @day.habit
end
def update
@habit = @day.habit
if @habit.update_attributes(params[:habit])
redirect_to @day
else
render "new"
end
end
def destroy
@habit = @day.habit
@habit.destroy
redirect_to @day
end
end
<h1>Life-ulator</h1>
<%= link_to "New Day", new_day_path %>
<table id="days-list">
<thead>
<tr>
<th>Date</th>
<th>Wake</th>
<th>Sleep</th>
</tr>
</thead>
<% @days.each do |day| %>
<tbody>
<tr>
<td><%= day.created_at.strftime("%x") %></td>
<td><%= day.wake %></td>
<td><%= day.sleep %></td>
<td><%= link_to "View", day_path(day) %></td>
<td><%= link_to "Edit", edit_day_path(day) %></td>
<td><%= link_to "Delete", day, :confirm => "Sure?", :method => :delete %></td>
</tr>
</tbody>
<% end %>
</table>
class Day < ActiveRecord::Base
attr_accessible :sleep, :wake
has_one :habit, :dependent => :destroy
end
class Habit < ActiveRecord::Base
belongs_to :day
attr_accessible :energy, :mood, :overall
end
LifeUlator::Application.routes.draw do
root :to => "Days#index"
resources :days do
resources :habits
end
end
<h1><%= @day.created_at.strftime("%x") %></h1>
<%= 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 "New Habits", new_day_habit_path(@day, @habit)%></td>
<td><%= link_to "Edit", edit_day_path(@day) %></td>
<td><%= link_to "Delete", @day, :confirm => "Sure?", :method => :delete %></td>
</tr>
</tbody>
</table>
<table id="habits-list">
<thead>
<tr>
<th>Energy</th>
<th>Mood</th>
<th>Overall</th>
<td><%= link_to "Delete", [@day, @habit], :confirm => "Sure?", :method => :delete %></td>
</tr>
</thead>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment