Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jurinco/552d4066c5e26183447d65aada9d0e5d to your computer and use it in GitHub Desktop.
Save jurinco/552d4066c5e26183447d65aada9d0e5d to your computer and use it in GitHub Desktop.
from the growing rails application pdf
class NotesController < ApplicationController
def index
load_notes
end
def show
load_note
end
def new
build_note
end
def create
build_note
save_note || render("new")
end
def edit
load_note build_note
end
def update
load_note
build_note
save_note || 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
redirect_to @note if @note.save
end
def note_params
params.require(:note).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