2013-10-31 31 views
0

我想延長我在西納特拉book.I看到一個例子想與DataMapper的嘗試,所以我結束了與此:404錯誤西納特拉應用程序中使用的DataMapper

require 'dm-core' 
require 'dm-migrations' 
require 'rubygems' 
require 'sinatra' 

configure:development do 
    DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/3sbase.db") 
    DataMapper.auto_upgrade! 
end 

class Artist 
    include DataMapper::Resource 
    property :name, String, :key=>true 
    property :born, Integer 
    has n, :songs 
end 

class Song 
    include DataMapper::Resource 
    property :id, Serial 
    property :title, String 
    property :year, Integer 
    property :singer, String 
    belongs_to :artist 
end 


DataMapper.finalize 


get '/songs' do 
    @songs = Song.all 
    slim :songs 
end 

get '/songs/new' do 
@song = Song.new 
slim :new_song 
end 

get '/songs/:id' do 
@song = Song.get(params[:id]) 
slim :show_songs 
end 

post '/songs' do 
song = Song.create(params[:song]) 
redirect to("/songs/#{song.id}") 
end 

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 

delete '/songs/:id' do 
Song.get(params[:id]).destroy 
redirect to('/songs') 
end 

的問題是,當我試圖創建一個新的歌曲到我的空數據庫我得到一個404錯誤後,我提供的歌曲的詳細信息,然後按保存按鈕。新網頁的鏈接不會像它應該//id擴展名。我實際上只改變了DataMapper的類,添加了「DataMapper.auto_upgrade!」並根據新的類屬性更改苗條文件。原來是:

configure:development do 
    DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db") 
end 

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 

DataMapper.finalize 

我懷疑,我很想念在路上的DataMapper的類work.Any什麼,我可能是做錯了想法,歡迎的東西。

我目前使用超薄文件

#new_song.slim 

h1 Add A New Song 
form method="POST" action="/songs" 
    == slim :song_form 


#song_form.slim 

label for="title" Title: 
input#title type="text" name="song[title]" value="#{@song.title}" 
label for="year" Released on: 
input#year type="text" name="song[year]" value="#{@song.year}" 
label for="singer" Performed by: 
input#singer type="text" name="song[singer]" value="#{@song.singer}" 
input type="submit" value="Save Song" 



/show_songs.slim 

h1= @song.title 
p Release Date: #{@song.year} 
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" 
+0

事實證明,這一切都發生,因爲DataMapper預計用戶也給藝術家屬性的值太我不在我的苗條文件中做的。我想這就是爲什麼id屬性保持零值,我得到404錯誤。 –

回答

0

事實證明,這一切是因爲DataMapper的期望用戶給出的值,以藝術家性質的東西太多,我不,我苗條的文件來執行。我想這就是爲什麼id屬性保持零值,我得到404錯誤。