Skip to content

Instantly share code, notes, and snippets.

@telagraphic
Created September 10, 2012 17:49
Show Gist options
  • Save telagraphic/3692486 to your computer and use it in GitHub Desktop.
Save telagraphic/3692486 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
@day = Day.find(params[:id])
@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
@day = Day.find(params[:id])
if @day.update_attributes(params[:day])
redirect_to days_path
else
render "edit"
end
end
def destroy
@day = Day.find(params[:id])
@day.destroy
redirect_to days_path
end
end
class HabitsController < ApplicationController
def index
end
def show
end
def new
@day = Day.find(params[:day_id])
@habit = @day.build_habit
end
def create
@day = Day.find(params[:day_id])
@habit = @day.build_habit(params[:habit])
if @habit.save!
redirect_to @day
else
render "new"
end
end
def edit
end
end
<%= form_for([@day, @habit]) do |h| %>
<%= h.label :energy %><br>
<%= h.text_field :energy %><br>
<%= h.label :mood %><br>
<%= h.text_field :mood %><br>
<%= h.label :overall %><br>
<%= h.text_area :overall, :size => "34x21" %><br><br>
<%= h.submit "Create" %>
<% end %>
class Day < ActiveRecord::Base
attr_accessible :sleep, :wake
has_one :habit
end
class Habit < ActiveRecord::Base
belongs_to :day
attr_accessible :energy, :mood, :overall
end
Life::Application.routes.draw do
root :to => "Days#index"
resources :days do
resources :habits
end
end
NoMethodError in Days#show
Showing /Users/tomlyons/Desktop/rails_projects/life/app/views/days/show.html.erb where line #38 raised:
undefined method `energy' for nil:NilClass
Extracted source (around line #38):
35:
36: <tbody>
37: <tr>
38: <td><%= @habit.energy %></td>
39: <td><%= @habit.mood %></td>
40: <td><%= @habit.overall %></td>
41: </tr>
Rails.root: /Users/tomlyons/Desktop/rails_projects/life
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment