设置好我的 config.ru 和 gemfile 后,我的帖子不再工作了。
首先,我必须在我的 main.rb 中有我的 DataMapper.setup,我不能在我的 irb 中运行任何 DataMapper 方法。
其次,每当我通过 POST 提交表单以将一些数据保存到我的 db Sinatra 时返回...
NoMethodError at /
undefined method `each' for "asdfasdf":String
file: resource.rb
location: attributes=
line: 329
主文件
#Required Gems
require 'sinatra'
require 'data_mapper'
DataMapper.setup(:default, ENV['DATABASE_URL'] || 'postgres://host:password!@localhost
/blog_development')
#Database Migration
class Code
include DataMapper::Resource
property :id, Serial
property :code, String, :required => true
property :translation, String
property :completed_at, DateTime
end
#Set up Migration
DataMapper.finalize
#Routes Section
get '/' do
@codes = Code.all
slim :index
end
#Get Code
get '/:code' do
@morse_code = params[:code]
slim :code
end
#POST Code
post '/' do
@code = Code.create params[:code]
redirect to('/')
end
宝石文件
source :rubygems
gem "sinatra", "~> 1.3.3"
gem "data_mapper", "~> 1.2.0"
gem "slim", "~> 1.3.4"
gem "dm-postgres-adapter", "~> 1.2.0"
配置.ru
require 'bundler'
Bundler.require
require './main'
DataMapper.setup(:default, ENV['DATABASE_URL'] || 'postgres://host:password@localhost
/blog_development')
run Sinatra::Application
请注意,asdfasdf 是我在提交之前在表单输入框中输入的文本。
对于回购,以防有人想对其进行现场测试...... https://github.com/thejourneydude/morsecode