Created
April 14, 2015 17:02
-
-
Save bootcoder/c63aa18876234fd1aeb7 to your computer and use it in GitHub Desktop.
restful notes controller
This file contains hidden or 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
# Add in base 7 routes | |
get '/notes' do | |
@notes = Note.all | |
erb :'notes/_index' | |
end | |
get '/notes/new' do | |
@note = Note.new | |
erb :'notes/_new' | |
end | |
get '/notes/:id' do | |
@note = Note.find(params[:id]) | |
erb :'notes/_show' | |
end | |
get '/notes/:id/edit' do | |
@note = Note.find(params[:id]) | |
erb :'notes/_edit' | |
end | |
post '/notes' do | |
@note = Note.create( | |
title: params[:title], | |
content: params[:content]) | |
if @note.save | |
redirect "/notes/#{@note.id}" | |
else | |
render :'notes/_new' | |
end | |
end | |
patch '/notes/:id' do | |
@note = Note.find(params[:id]) | |
@note.update_attributes( | |
title: params[:title], | |
content: params[:content]) | |
if @note.save | |
redirect "/notes#{@note.id}" | |
else | |
render :'notes/_edit' | |
end | |
end | |
delete '/notes/:id' do | |
Note.find(params[:id]).destroy | |
redirect '/notes' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment