gpt4 book ai didi

ruby - Sinatra 变量作用域

转载 作者:数据小太阳 更新时间:2023-10-29 06:47:50 25 4
gpt4 key购买 nike

采用以下代码:

### Dependencies
require 'rubygems'
require 'sinatra'
require 'datamapper'

### Configuration
config = YAML::load(File.read('config.yml'))

name = config['config']['name']
description = config['config']['description']
username = config['config']['username']
password = config['config']['password']
theme = config['config']['theme']

set :public, 'views/themes/#{theme}/static'

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

class Post
include DataMapper::Resource
property :id, Serial
property :name, String
property :body, Text
property :created_at, DateTime
property :slug, String
end

class Page
include DataMapper::Resource
property :id, Serial
property :name, String
property :body, Text
property :slug, String
end

DataMapper.auto_migrate!

### Controllers
get '/' do
@posts = Post.get(:order => [ :id_desc ])
haml :"themes/#{theme}/index"
end

get '/:year/:month/:day/:slug' do
year = params[:year]
month = params[:month]
day = params[:day]
slug = params[:slug]

haml :"themes/#{theme}/post.haml"
end

get '/:slug' do
haml :"themes/#{theme}/page.haml"
end

get '/admin' do
haml :"admin/index.haml"
end

我想让 name 和所有这些变量对整个脚本以及 View 可用。我试着让它们成为全局变量,但没有成功。

最佳答案

可能不是“最干净”的方式,但将它们设置为选项应该可行:
--> http://www.sinatrarb.com/configuration.html :)

设置:

set :foo, 'bar'

获得:

"foo is set to " + settings.foo

关于ruby - Sinatra 变量作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2135854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com