Sinatra没有方法错误

时间:2012-12-17 23:16:19

标签: ruby rubygems sinatra bundler ruby-datamapper

设置我的config.ru和gemfile后,我的帖子不再想要工作了。

首先,我必须在main.rb中使用我的DataMapper.setup,并且我无法在我的irb中运行任何DataMapper方法。

其次,每当我通过POST提交表格时,一些数据都会保存到我的数据库中,Sinatra将返回......

NoMethodError at /
undefined method `each' for "asdfasdf":String

file: resource.rb
location: attributes=
line: 329

main.rb的

#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

的Gemfile

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"

config.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

1 个答案:

答案 0 :(得分:1)

您正在呼叫Code.create params[:code]。试试Code.create :code => params[:code]。这样,字符串不会在datamapper期望属性哈希的地方结束。