Skip to content

Instantly share code, notes, and snippets.

@stereosupersonic
Last active May 28, 2017 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stereosupersonic/35042a1623f0c5d9a8fc to your computer and use it in GitHub Desktop.
Save stereosupersonic/35042a1623f0c5d9a8fc 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