Created
March 8, 2016 19:58
-
-
Save bootcoder/5254200ddffa269d93ad to your computer and use it in GitHub Desktop.
basic RESTful example (dogs)
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
#dogs Controller | |
# dogs INDEX | |
get '/dogs' do | |
@dogs = dog.all | |
erb :'dogs/index' | |
end | |
# dogs SHOW | |
get '/dogs/:id' do | |
@dog = Dog.find(params[:id]) | |
erb :'dogs/show' | |
end | |
# dogs EDIT | |
get '/dogs/:id/edit' do | |
@dog = Dog.find(params[:id]) | |
erb :'dogs/edit' | |
end | |
# dogs NEW | |
get '/dogs/new' do | |
@dog = Dog.new | |
erb :'dogs/new' | |
end | |
# dogs PUT | |
put '/dogs/:id' do | |
@dog = Dog.find(params[:id]) | |
@dog.update(params[:dog_info]) | |
redirect "/dogs/#{@dog.id}" | |
end | |
# dogs POST | |
post '/dogs' do | |
@dog = Dog.new(params[:dog_info]) | |
if @dog.save | |
redirect '/dogs' | |
else | |
flash[:error] = @dog.errors.full_messages | |
redirect '/dogs/new' | |
end | |
end | |
# dogs DELETE | |
delete '/dogs/:id' do | |
@dog = Dog.find(params[:id]) | |
@dog.destroy | |
redirect '/dogs' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment