Sinatra App不更新数据库

时间:2011-08-19 02:10:14

标签: sqlite sinatra datamapper

我正在尝试创建一个简单的webapp来记录学校的笔记,但数据库没有像我认为的那样更新。这是代码。

Sinatra app:

require 'rubygems'
require 'sinatra'
require 'datamapper'

DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/school.db")

class Note
  include DataMapper::Resource
  property :id, Serial
  property :class, Text, :required => true
  property :title, Text, :required => true
  property :content, Text, :required => true
  property :created_at, DateTime
  property :updated_at, DateTime
end

DataMapper.finalize.auto_upgrade!

get '/school' do
  #
  # @notes is nil
  #

  @notes = Note.all
  @title = 'Notes'
  erb :school
end

get '/' do
  @title = 'Home'
  erb :index
end

get '/addnote' do
  erb :addnote
end

post '/addnote' do
  n = Note.new
  n.class = params[:class]
  n.content = params[:content]
  n.created_at = Time.now
  n.updated_at = Time.now
  n.save

  redirect '/school'
end

这是表格

<section id="add">
  <form action="/addnote" method="post">
    <input type="text" name="class" placeholder="Class" />
    <input type="text" name="title" placeholder="Title" />
    <textarea name="content"></textarea>
    <input type="submit" value="Take Note"/>
  </form>
</section>

由于

1 个答案:

答案 0 :(得分:2)

我猜您忘记在title路线中设置post /addnote属性。

它在模型中声明为required,但未设置。

尝试将此添加到您的路线以查看是否已保存:

if n.save
  redirect '/school'
else
  #show an error or something
end