-
-
Save jurinco/552d4066c5e26183447d65aada9d0e5d to your computer and use it in GitHub Desktop.
from the growing rails application pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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