0

我正在整理我的第一个 Sinatra 应用程序,但我遇到了数据库关系问题。我已经尝试了所有我知道该怎么做的事情(不多),但我仍在摸不着头脑。

这是我不断收到的错误:

    <DataObjects::SyntaxError: 
    ERROR: relation "lovers" does not exist 
    LINE 1: ...ug", "lover_name", "last_updated", "sidebar" 
    FROM "lovers" W... ^ (code: 16908420, sql state: 42P01, query: 
    SELECT "id", "slug", "lover_name", "last_updated", "sidebar" 
    FROM "lovers" 
    WHERE "slug" = 'home' 
    ORDER BY "id" 
    LIMIT 1, 
    uri: postgres:localhost/keeprdb?scheme=postgres&user=&password=&host=localhost&port=&path=/keeprdb&query=&fragment=&adapter=postgres)>

这是 Git Hub 信息 github.com/Jonnyread/keepr

对于我如何解决这个问题的任何帮助将不胜感激。

如果您需要我的其他任何东西,请随时询问。

由于我不确定相关代码是什么,所以这里是 application.rb 文件:

    require 'rubygems'
    require 'sinatra'
    require 'sinatra/flash'
    require 'dm-core'
    require 'dm-validations'
    require 'dm-types'
    require 'dm-migrations'
    require 'dm-postgres-adapter'
    require 'rdiscount'
    require 'rack-ssl-enforcer'

    DataMapper.setup(:default, ENV['DATABASE_URL'] || 'postgres://localhost/keeprdb')

    class Lover 
      include DataMapper::Resource
      property :id,           Serial
      property :slug,         String
      property :lover_name,   String
      property :content,      Text
      property :last_updated, DateTime  
      property :sidebar,      Enum[ :yes, :no ], :default => :no
    end

    DataMapper.finalize

    # Global variables
    NAV_TITLE = 'Keepr'
    FOOTER = '<em>"This page is maintained by that wild demi-god Jahmed."</em> -                 <strong>All Praises</strong>'

    # Authentication
    # use Rack::Auth::Basic, "Restricted Area" do |username, password|
    #   [username, password] == [username, password]
    # end

    # Force all connections to use SSL
    # use Rack::SslEnforcer

    # Converts page into post slug
    def slugify(content)
        content.downcase.gsub(/ /, '-').gsub(/{^a-z0-9_-}/, '').squeeze('-')
    end

    # Sets index page as "home"
    get '/' do
        redirect '/home/'
    end

    # Creates a new lover from "new lover" form

    post '/' do
        if params[:userinput].empty?
            redirect '/'
        else
        @lover = Lover.first_or_create(:lover_name => params[:userinput], :slug => slugify(params[:userinput]))
        @lover.content = "This is a new lover.  Congrats!  You should write what you remember about her.  That is of course if you remember - you animal."
        @lover.last_updated = DateTime.now
        @lover.save
        flash[:notice] = "Lover created successfully.  Hope you're that successful too"
        redirect "/#{@lover.slug}/"
        end
    end

    # List all lovers in the database
    get '/all/' do
        @lover = Lover.new
        @lover.lover_name = 'All Lovers'
        @lover.slug = 'all'
        @lovers = Lover.all
        @sidebars = Lover.all(:sidebar => 'yes')
        erb :all
    end

    # Displays requested lover
    get '/:url/' do
        @lover = Lover.first(:slug => params[:url])
        if @lover == nil
            flash[:notice] = "That chick don't exist boy."
            redirect '/home/'
        else
            @lovers = Lover.all
            @sidebars = Lover.all(:sidebar => 'yes')
            erb :show
        end
    end

    # Edits requested note
    get '/:url/edit' do
      @lover = Lover.first(:slug => params[:url])
      @sidebars = Lover.all(:sidebar => 'yes')
      erb :edit
    end

    # Saves user edits to a lover
    post '/:url/edit' do
        @lover = Lover.first(:slug => params[:url])
        @lover.lover_name = params[:lover_name]
        @lover.content = params[:content]
        @lover.sidebar = params[:sidebar]
@lover.last_updated = DateTime.now
if lover.slug != 'home'
    @lover.slug = slugify(params[:lover_name])
end
@lover.save
flash[:notice] = "Change to Lover is saved. Bitch."
redirect "/#{@lover.slug}/"
    end

    # Readies requested lover for deletion
    get '/:url/delete' do
        @lover = Lover.first(:slug => params[:url])
        @lovers = Lover.all
        @sidebars = Lover.all(:sidebar => 'yes')
        erb :delete
    end

    # Deletes specified lover
    delete '/:url/delete' do
        @lover = Lover.first(:slug => params[:url])
        @lover.destroy
        flash[:notice] = 'You just dropped that bitch.  You should probably go out and find another.'
        redirect '/home/'
    end

    # Redirects user to homepage if a lover is not found
    not_found do
        flash[:notice] = "Dude, we all know you didn't book that girl.  Straight up."
        redirect '/home'
    end

这是错误所在的部分:

    # Displays requested lover
    get '/:url/' do
        @lover = Lover.first(:slug => params[:url])
        if @lover == nil
            flash[:notice] = "That chick don't exist boy."
            redirect '/home/'
        else
            @lovers = Lover.all
            @sidebars = Lover.all(:sidebar => 'yes')
            erb :show
        end
    end

这是 Rakefile:

    require 'rubygems'
    require 'sinatra'
    require 'sinatra/flash'
    require 'dm-core'
    require 'dm-validations'
    require 'dm-types'
    require 'dm-migrations'
    require 'dm-postgres-adapter'
    require 'rdiscount'
    require 'rack-ssl-enforcer'

    DataMapper.setup(:default, ENV['DATABASE_URL'] || 'postgres://localhost/keeprdb')

    class Lover 
      include DataMapper::Resource
      property :id,           Serial
      property :slug,         String
      property :lover_name,   String
      property :content,      Text
      property :last_updated, DateTime  
      property :sidebar,      Enum[ :yes, :no ], :default => :no
    end

    DataMapper.auto_migrate!

    task :setup do 
        @lover = Lover.create(:slug => 'home')
        @lover.slug = 'home'
        @lover.lover_name = 'Home'
        @lover.content = 'This is the beginning of the next step my man. -Jahmed'
        @lover.last_updated = DateTime.now
        @lover.sidebar = :no
        @lover.save 
    end

希望这可以帮助。

再次感谢你的帮助。

4

1 回答 1

0

在您的application.rb源代码中,紧接着该行

DataMapper.finalize

添加行

DataMapper.auto_upgrade!

::auto_upgrade!尝试使数据库模式与您的模型匹配。它CREATE是新表,并根据需要向现有表添加列。有关详细信息,请参阅:http ://datamapper.org/getting-started.html

这能解决吗?

于 2013-07-05T22:29:49.310 回答