Gem Camping 的 Nuts::Models.create_schema 不生成数据库表

tablecell · 2020年08月22日 · 最后由 tablecell 回复于 2021年08月28日 · 1071 次阅读

参考 https://github.com/camping/camping/blob/master/book/02_getting_started.md 的例子

When The Camping Server boots up, it will automatically call Nuts.create

在代码中加上

def Nuts.create
  Nuts::Models.create_schema
end

是初始化数据库表 但是 执行

camping ntuts.rb

可以访问模板页面,读数据库提示

SQLite3::SQLException: no such table: nuts_pages

在当前目录生成一个 nuts.db 手工建表也一样的提示

测试代码

require "camping"
require "camping/ar"

Camping.goes :Nuts

module Nuts
  module Models
    class Page < Base
    end

    class BasicFields < ActiveRecord::Migration[4.2] # < V 1.0
      def self.up
        create_table Nuts.page do |t|
          t.string :title
          t.text :content
          # This gives us created_at and updated_at
          t.timestamps
        end
      end

      def self.down
        drop_table Nuts.posts
      end
    end
  end

  module Controllers
    module Nuts::Controllers
      class Index < R "/"
        def get
          @pages = Page.all #(:select => "title")
          render :list
          #  Time.now.to_s
        end
      end
    end
  end

  module Views
    def layout
      html do
        head do
          title { "Nuts And GORP" }
        end
        body { self << yield }
      end
    end

    def list
      h1 "All pages"
      ul do
        @pages.each do |page|
          li do
            a page.title, :href => R(PageX, page.title)
          end
        end
      end
    end
  end
end

def Nuts.create
  Nuts::Models.create_schema
end
tablecell 关闭了讨论。 08月22日 07:50
tablecell 重新开启了讨论。 08月22日 07:50
tablecell 重新开启了讨论。 08月22日 07:50

这都死多少年了的框架

class Options
    if home = ENV['HOME'] # POSIX
      DB = File.join(home, '.camping.db')
      RC = File.join(home, '.campingrc')
    elsif home = ENV['APPDATA'] # MSWIN
      DB = File.join(home, 'Camping.db')
      RC = File.join(home, 'Campingrc')
    else
      DB = nil
      RC = nil
    end

数据库是从环境变量中拼出来的 框架死了,只要有代码,就还能跑起来

需要 登录 后方可回复, 如果你还没有账号请 注册新账号