Reputation: 11107
After setting up my config.ru and gemfile, my post no longer wants to work.
First, I have to have my DataMapper.setup in my main.rb and I cannot run any DataMapper methods in my irb.
Second, whenever I submit a form via POST for some data to be saved to my db Sinatra returns with...
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
Note that asdfasdf is the text I put in the input box for the form before submit.
For the repo in case anyone wants to live test it... https://github.com/thejourneydude/morsecode
Upvotes: 0
Views: 799
Reputation: 1042
You are calling Code.create params[:code]
. Try Code.create :code => params[:code]
. This way the string does not end up where datamapper expects an attribute hash.
Upvotes: 1