Skip to content

Instantly share code, notes, and snippets.

@jamesgecko
Created March 16, 2012 21:30
Show Gist options
  • Save jamesgecko/2052860 to your computer and use it in GitHub Desktop.
Save jamesgecko/2052860 to your computer and use it in GitHub Desktop.
class Note < ActiveRecord::Base
belongs_to :customer
attr_accessible :content
end
# == Schema Information
#
# Table name: notes
#
# id :integer not null, primary key
# content :text
# customer_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
class NotesController < ApplicationController
def show
@note = Note.find(params[:id])
#@customer = Customer.find(@note.customer_id) # This works commented out? Bwa?
respond_to do |format|
format.html
format.json {render json: @note }
end
end
# GET /notes/new
# GET /notes/new.json
def new
@note = Note.new
@customer = Customer.find(params[:customer_id])
respond_to do |format|
format.html # new.html.erb
format.json { render json: @note }
end
end
# GET /notes/1/edit
def edit
@note = Note.find(params[:id])
@customer = Customer.find(@note.customer_id)
end
# POST /notes
# POST /notes.json
def create
@customer = Customer.find(params[:customer_id])
@note = @customer.notes.build(params[:note])
respond_to do |format|
if @note.save
format.html { redirect_to @customer, notice: 'Note was successfully created.' }
format.json { render json: @note, status: :created, location: @note }
else
format.html { render action: "new" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# PUT /notes/1
# PUT /notes/1.json
def update
@note = Note.find(params[:id])
@customer = Customer.find(@note.customer_id)
respond_to do |format|
if @note.update_attributes(params[:note])
format.html { redirect_to @customer, notice: 'Note was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @note.errors, status: :unprocessable_entity }
end
end
end
# DELETE /notes/1
# DELETE /notes/1.json
def destroy
@note = Note.find(params[:id])
@note.destroy
respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
end
end
<p id="notice"><%= notice %></p>
<p>
<b>Last updated:</b>
<%= @note.updated_at %>
</p>
<p>
<%= @note.content %>
</p>
<%= link_to 'Edit', edit_note_path(@note) %> |
<%= link_to 'Back', customer_path(@customer) %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment