I am trying to extend an example I saw in a sinatra book.I wanted to experiment with DataMapper so I ended up with this:

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

The problem is that when I try to create a new song to my empty database I get a 404 Error after I give the details of the song and press save button.The link of the new page does not take the /:id extension as it was supposed. I have actually only changed the class of the DataMapper, added the "DataMapper.auto_upgrade!" and changed the slim files according to the new class properties. The original was :

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

I suspect that I am missing something in the way DataMapper's classes work.Any ideas of what I might be doing wrong are welcome.

The slim files I am currently using are

    #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"
有帮助吗?

解决方案

It turns out that it all happens because DataMapper expects the user to give values to the Artist properties too something I don't do in my slim file. I guess this is why the id property maintains the nil value and I get the 404 Error.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top