Skip to content

Instantly share code, notes, and snippets.

@knoxjeffrey
Created February 25, 2015 17:39
Show Gist options
  • Save knoxjeffrey/920f1a0431ad5412dd4e to your computer and use it in GitHub Desktop.
Save knoxjeffrey/920f1a0431ad5412dd4e to your computer and use it in GitHub Desktop.
class NotesController < ApplicationController
def index
load_notes
end
def show
load_note
end
def new
build_note
end
def create
build_note
save_note or render 'new'
end
def edit
load_note
build_note
end
def update
load_note
build_note
save_note or render 'edit'
end
def destroy
load_note
@note.destroy
redirect_to notes_path
end
private
def load_notes
@notes ||= note_scope.to_a
end
def load_note
@note ||= note_scope.find(params[:id])
end
def build_note
@note ||= note_scope.build @note.attributes = note_params
end
def save_note
if @note.save
redirect_to @note
end
end
def note_params
note_params = params[:note]
note_params ? note_params.permit(:title, :text, :published) : {}
end
def note_scope
Note.scoped
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment