Skip to content

Instantly share code, notes, and snippets.

@jamesgecko
Created March 19, 2012 20:53
Show Gist options
  • Save jamesgecko/2127024 to your computer and use it in GitHub Desktop.
Save jamesgecko/2127024 to your computer and use it in GitHub Desktop.
Bill#update_date_paid method not found?
#app/models/bill.rb
class Bill < ActiveRecord::Base
belongs_to :customer
attr_accessible :amount, :date_due, :paid
before_create :default_values
before_save self.update_date_paid
def default_values
self.paid ||= false
end
def self.find_pending
where(:paid => false)
end
def self.find_paid
where(:paid => true)
end
def self.update_date_paid
if self.paid && self.date_paid.nil?
write_attribute(:date_paid, Time.now)
elsif self.paid == false && !self.date_paid.nil?
write_attribute(:date_paid, nil)
end
end
end
# == Schema Information
#
# Table name: bills
#
# id :integer not null, primary key
# customer_id :integer
# amount :integer
# date_paid :datetime
# paid :boolean
# created_at :datetime not null
# updated_at :datetime not null
# date_due :date
#
#app/controllers/bills_controller.rb
class BillsController < ApplicationController
# GET /bills/new
# GET /bills/new.json
def new
@bill = Bill.new
@customer = Customer.find(params[:customer_id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: @bill }
end
end
# GET /bills/1/edit
def edit
@bill = Bill.find(params[:id])
@customer = Customer.find(@bill.customer_id)
end
# POST /bills
# POST /bills.json
def create
@customer = Customer.find(params[:customer_id])
@bill = @customer.bills.build(params[:bill])
respond_to do |format|
if @bill.save
format.html { redirect_to @customer, notice: 'Bill was successfully created.' }
format.json { render json: @bill, status: :created, location: @bill }
else
format.html { render action: "new" }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
# PUT /bills/1
# PUT /bills/1.json
def update
@bill = Bill.find(params[:id])
@customer = Customer.find(@bill.customer_id)
respond_to do |format|
if @bill.update_attributes(params[:bill])
format.html { redirect_to @customer, notice: 'Bill was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @bill.errors, status: :unprocessable_entity }
end
end
end
# DELETE /bills/1
# DELETE /bills/1.json
def destroy
@bill = Bill.find(params[:id])
@bill.destroy
respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
end
end
#app/controllers/customers_controller.rb
class CustomersController < ApplicationController
# GET /customers
# GET /customers.json
def index
@customers = Customer.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @customers }
end
end
# GET /customers/1
# GET /customers/1.json
def show
@customer = Customer.find(params[:id])
@phones = @customer.phones.find(:all)
@emails = @customer.emails.find(:all)
@pending_bills = @customer.bills.find_pending()
@paid_bills = @customer.bills.find_paid()
@notes = @customer.notes.find(:all)
respond_to do |format|
format.html # show.html.erb
format.json { render json: @customer }
end
end
# GET /customers/new
# GET /customers/new.json
def new
@customer = Customer.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @customer }
end
end
# GET /customers/1/edit
def edit
@customer = Customer.find(params[:id])
end
# POST /customers
# POST /customers.json
def create
@customer = Customer.new(params[:customer])
respond_to do |format|
if @customer.save
format.html { redirect_to @customer, notice: 'Customer was successfully created.' }
format.json { render json: @customer, status: :created, location: @customer }
else
format.html { render action: "new" }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
# PUT /customers/1
# PUT /customers/1.json
def update
@customer = Customer.find(params[:id])
respond_to do |format|
if @customer.update_attributes(params[:customer])
format.html { redirect_to @customer, notice: 'Customer was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
# DELETE /customers/1
# DELETE /customers/1.json
def destroy
@customer = Customer.find(params[:id])
@customer.destroy
respond_to do |format|
format.html { redirect_to customers_url }
format.json { head :no_content }
end
end
end
http://localhost:3000/customers/1
NoMethodError in CustomersController#show
undefined method `update_date_paid' for #<Class:0x9f72a34>
Rails.root: /vagrant/billing
Application Trace | Framework Trace | Full Trace
activerecord (3.2.1) lib/active_record/dynamic_matchers.rb:50:in `method_missing'
app/models/bill.rb:5:in `<class:Bill>'
app/models/bill.rb:1:in `<top (required)>'
activesupport (3.2.1) lib/active_support/dependencies.rb:469:in `load'
activesupport (3.2.1) lib/active_support/dependencies.rb:469:in `block in load_file'
activesupport (3.2.1) lib/active_support/dependencies.rb:639:in `new_constants_in'
activesupport (3.2.1) lib/active_support/dependencies.rb:468:in `load_file'
activesupport (3.2.1) lib/active_support/dependencies.rb:353:in `require_or_load'
activesupport (3.2.1) lib/active_support/dependencies.rb:502:in `load_missing_constant'
activesupport (3.2.1) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.1) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.1) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.1) lib/active_support/dependencies.rb:514:in `load_missing_constant'
activesupport (3.2.1) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.1) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.1) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.1) lib/active_support/inflector/methods.rb:229:in `block in constantize'
activesupport (3.2.1) lib/active_support/inflector/methods.rb:228:in `each'
activesupport (3.2.1) lib/active_support/inflector/methods.rb:228:in `constantize'
activesupport (3.2.1) lib/active_support/dependencies.rb:554:in `get'
activesupport (3.2.1) lib/active_support/dependencies.rb:588:in `constantize'
activerecord (3.2.1) lib/active_record/inheritance.rb:111:in `block in compute_type'
activerecord (3.2.1) lib/active_record/inheritance.rb:109:in `each'
activerecord (3.2.1) lib/active_record/inheritance.rb:109:in `compute_type'
activerecord (3.2.1) lib/active_record/reflection.rb:172:in `klass'
activerecord (3.2.1) lib/active_record/associations/association.rb:117:in `klass'
activerecord (3.2.1) lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
app/controllers/customers_controller.rb:19:in `show'
actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.1) lib/active_support/callbacks.rb:414:in `_run__373879947__process_action__6850445__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `dispatch'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in `call'
journey (1.0.2) lib/journey/router.rb:68:in `block in call'
journey (1.0.2) lib/journey/router.rb:56:in `each'
journey (1.0.2) lib/journey/router.rb:56:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__304140726__call__647857879__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.2.1) lib/rails/engine.rb:479:in `call'
railties (3.2.1) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/home/vagrant/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/home/vagrant/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/vagrant/.rbenv/versions/1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Request
Parameters:
{"id"=>"1"}
Show session dump
_csrf_token: "AZaUwGgrlUixvzFkXWlcntCINvgAKqoziT1AavoozDA="
session_id: "578d5c507786407d76c29ae6481e7009"
Show env dump
GATEWAY_INTERFACE: "CGI/1.1"
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
HTTP_ACCEPT_CHARSET: "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
HTTP_ACCEPT_ENCODING: "gzip,deflate,sdch"
HTTP_ACCEPT_LANGUAGE: "en-US,en;q=0.8"
HTTP_CACHE_CONTROL: "max-age=0"
REMOTE_ADDR: "10.0.2.2"
REMOTE_HOST: "10.0.2.2"
SERVER_NAME: "localhost"
SERVER_PROTOCOL: "HTTP/1.1"
Response
Headers:
None
#app/views/customers/show.html.erb
<h2><div><%= @customer.name %></div></h2>
<div class="row-fluid">
<div class="span4">
<p>
<h4>Address:</h4>
<%= @customer.address %>
<br>
<%= @customer.county %>, <%= @customer.state %> <%= @customer.zip %><br>
<%= link_to 'Map', street_map_url("#{@customer.address} #{@customer.county}, #{@customer.state} #{@customer.zip}"), target: "_blank" %>
</p>
<p>
<h4>Phone numbers</h4>
<% if @phones.empty? %>
No phone number
<% else %>
<table class="table table-striped">
<thead>
<tr>
<th>Number</th>
<th>Type</th>
<th></th>
</tr>
</thead>
<% @phones.each do |phone| %>
<tr>
<td><%= phone.number %></td>
<td><%= phone.number_type_name %></td>
<td><%= link_to 'X', phone_path(phone), confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end -%>
</table>
<% end -%>
<%= link_to 'Add', new_customer_phone_path(@customer), class: 'btn' %>
</p>
<p>
<h4>Email addresses</h4>
<% if @emails.empty? %>
No email address
<% else %>
<table class="table table-striped">
<thead>
<tr>
<th>Address</th>
<th>Type</th>
<th></th>
</tr>
</thead>
<% @emails.each do |email| %>
<tr>
<td><%= email.address %></td>
<td><%= email.address_type_name %></td>
<td><%= link_to 'X', email_path(email), confirm: 'Are you sure?', method: :delete, class: 'btn-danger' %></td>
</tr>
<% end -%>
</table>
<% end -%>
<%= link_to 'Add', new_customer_email_path(@customer), class: 'btn' %>
</p>
</div>
<div class="span8">
<p>
<h4>Unpaid bills</h4><br>
<% if @pending_bills.empty? %>
No pending bills
<% else %>
<table class="table table-striped">
<thead>
<tr>
<th>Amount</th>
<th>Due date</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<% @pending_bills.each do |bill| %>
<tr>
<td><%= bill.amount %></td>
<td><%= bill.date_due %></td>
<td><%= link_to 'Edit', edit_bill_path(bill) %></td>
<td><%= link_to 'Mark paid', bill_path(bill,
bill: { paid: true }),
method: :put %>
</td>
<td><%= link_to 'Remove', bill_path(bill),
confirm: 'Are you sure?',
method: :delete %>
</td>
</tr>
<% end -%>
</table>
<% end -%>
<%= link_to 'Add', new_customer_bill_path(@customer), class: 'btn' %>
</p>
<p>
<h4>Paid bills</h4><br>
<% if @paid_bills.empty? %>
No pending bills
<% else %>
<table class="table table-striped">
<thead>
<tr>
<th>Amount</th>
<th>Due date</th>
<th>Paid on</th>
<th></th>
</tr>
</thead>
<% @paid_bills.each do |bill| %>
<tr>
<td><%= bill.amount %></td>
<td><%= bill.date_due %></td>
<td><%= bill.date_paid %></td>
<td></td>
</tr>
<% end -%>
</table>
<% end -%>
</p>
</div>
</div>
<hr />
<p>
<h4>Notes</h4><br>
<% if @notes.empty? %>
No notes
<% else %>
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Last modified</th>
<th></th>
</tr>
</thead>
<% @notes.each do |note| %>
<tr>
<td><%= link_to summarise(note.content), edit_note_path(note) %></td>
<td><%= note.updated_at %></td>
<td><%= link_to 'X', note_path(note), confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end -%>
</table>
<% end -%>
<br />
<%= link_to 'Add', new_customer_note_path(@customer), class: 'btn' %>
</p>
<hr />
<%= link_to 'Edit customer', edit_customer_path(@customer), class: 'btn' %>
<%= link_to 'Back', customers_path, class: 'btn' %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment