0

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

thank_you
  • 11,001
  • 19
  • 101
  • 185
  • and `"asdfasdf":String` comes from where? – shime Dec 18 '12 at 00:43
  • It's the text I put in the input box of the form before I submitted it. – thank_you Dec 18 '12 at 00:47
  • I'm almost certain you're doing something wrong with data mapper. Why can't you use it in irb? What is `resource.rb` and what's on the line 329? I also recommend that you remove your DB credentials from here ;) – shime Dec 18 '12 at 00:57
  • Haha, I make sure the password I use for my DB doesn't fit any other password I use, EVER, but thanks for the tip. I have no idea what `resource.rb` is. It's not in my directory. My irb tells me either I need to do DataMapper.setup, which is in my config.ru (if I move this to main.rb the error goes away and I can use it). I keep thinking this is a bundler issue. – thank_you Dec 18 '12 at 01:02
  • 1
    :D ok. if you put all that's necessary to reproduce this in a gist and mail it to me or put it here, I can find out what's causing this trouble for you after I catch some sleep. – shime Dec 18 '12 at 01:06
  • https://github.com/thejourneydude/morsecode – thank_you Dec 18 '12 at 01:07

1 Answers1

1

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.

mbj
  • 1,042
  • 9
  • 19