2012-12-17 110 views
0

設置我的config.ru和gemfile後,我的帖子不再想工作。Sinatra沒有方法錯誤

首先,我必須在我的main.rb中有我的DataMapper.setup,並且我無法在我的irb中運行任何DataMapper方法。

其次,每當我通過POST提交表單的一些數據保存到我的分貝西納特拉返回與...

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:[email protected] 
/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:[email protected] 

/blog_development') 

run Sinatra::Application 

請注意,asdfasdf是我在提交前放入表單的輸入框中的文本。

對於萬一有人願意住測試回購... https://github.com/thejourneydude/morsecode

+0

和''asdfasdf「:String'來自哪裏? – shime

+0

這是我在提交表單之前在表單的輸入框中輸入的文本。 – jason328

+0

我幾乎可以肯定你在做數據映射器的問題。爲什麼你不能在irb中使用它?什麼是'resource.rb'和329行是什麼?我也建議你從這裏刪除你的數據庫憑證;) – shime

回答

1

要調用Code.create params[:code]。嘗試Code.create :code => params[:code]。這樣,字符串不會在datamapper期望屬性散列的地方結束。

+0

謝謝。有用!! – jason328