Last active
August 29, 2015 14:15
-
-
Save TiagoTi/22ac5768ed5611865320 to your computer and use it in GitHub Desktop.
RestFull Routes and Templeates Slim With DataMapper
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 Song | |
include DataMapper::Resource | |
property :id, Serial | |
property :title, String | |
property :lyrics, Text | |
property :length, Integer | |
property :released_on, Date | |
def released_on=date | |
super Date.strptime(date, '%m/%d/%Y') | |
end | |
end |
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
#Listing Songs | |
get '/songs' do | |
@songs = Song.all | |
slim :songs | |
end | |
#Showing Songs | |
get '/songs/:id' do | |
@song = Song.get(params[:id]) | |
slim :show_song | |
end | |
#Creating New Songs | |
#deve ser antes da Showing pq /:id vai tentar interpletar o new como parametro | |
get '/songs/new' do | |
@song = Song.new | |
slim :new_song | |
end | |
post '/songs' do | |
song = Song.create(params[:song]) | |
redirect to("/songs/#{song.id}") | |
end | |
#Editing Songs | |
get '/songs/:id/edit' do | |
@song = Song.get(params[:id]) | |
slim :edit_song | |
end | |
put '/songs/:id' do | |
song = Song.get(params[:id]) | |
song.update(params[:song]) | |
redirect to("/songs/#{song.id}") | |
end | |
#Deleting Songs | |
delete '/songs/:id' do | |
Song.get(params[:id]).destroy | |
redirect to('/songs') | |
end |
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
h1 Songs | |
a href="/songs/new" Create a new song | |
- if @songs.any? | |
ul#songs | |
-@songs.each do |song| | |
li <a href="/songs/#{song.id}">#{song.title}</a> | |
- else | |
p No songs have been created yet! |
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
h1= @song.title | |
p Release Date: #{@song.released_on.strftime("%e %B %Y") if @song.released_on} | |
p Length: #{@song.length/60} minutes #{@song.length%60} seconds | |
pre= @song.lyrics | |
p <a href="/songs">back to songs index</a> | |
p <a href="/songs/#{@song.id}/edit">edit this song</a> | |
form action="/songs/#{@song.id}" method="POST" | |
input type="hidden" name="_method" value="DELETE" | |
input type="submit" value="delete this song" |
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
h1 Add A New Song | |
form method="POST" action="/songs" | |
== slim :song_form |
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
label for="title" Title: | |
input#title type="text" name="song[title]" value="#{@song.title}" | |
label for="length" Length: | |
input#length type="number" name="song[length]" value="#{@song.length}" | |
label for="released_on" Date(mm/dd/yyyy): | |
input#released_on type="text" name="song[released_on]" value="#{@song.released_on.strftime("%m/%d/%Y") if @song.released_on}" | |
label for="lyrics" Lyrics: | |
textarea#lyrics name="song[lyrics]" == @song.lyrics | |
input type="submit" value="Save Song" |
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
h1 Edit Song | |
form method="POST" action="/songs/#{@song.id}" | |
input type="hidden" name="_method" value="PUT" | |
== slim :song_form |
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
$ -> | |
$('#released_on') | |
.datepicker( changeYear: true, yearRange: '1940:2000' ) | |
$('#like input').click (event) -> | |
event.preventDefault() | |
$.post( | |
$('#like form').attr('action') | |
(data) -> $('#like p').html(data) | |
.effect('highlight', color: '#fcd') | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment