I am using ruby 1.9.2 and sinatra 1.3.2, Datamapper
I get error: “can’t convert nil into Hash”
in /new_record i have a form, and when submitting the form (with some blank fields) instead of getting validation messages i get this error!
here post ‘/new_record’ – because I populating this form with some values of previous form
post '/new_record' do
@projects = Project.all
@date = DateTime.parse(params[:report][:date])
erb :new_record
end
post '/create_record' do
@user = User.get(current_user.id)
if @user.records.create(params[:record])
flash(:notice => "Report created successfully!")
redirect "/"
else
@projects = Project.all
@date = DateTime.parse(params[:report][:date])
erb :new_record
end
I can’t guess why because similar form works well:
get '/new' do
#session[:user_id] = nil
title "Create new account"
erb :new_user
end
post '/create' do
@user = User.new(params[:user])
if @user.save
session[:user_id] = nil
redirect "/"
else
erb :new_user
end
end
I think it is something with @user.records.create(params[:record]) with create method, that it tries to build hash, but why if it can’t build a hash just don’t throw error that it couldn’t save (build) @user.records dependency ?
Is there any way to work around this? Because I would like to build record using relationship.
Thanks in advice!
DataMapper is not throwing an error that it couldn’t save because it detects a problem even before it tries to save the record. (Also notice that, by default, DataMapper doesn’t throw exceptions anyway — you have to enable it.)
createtakes a hash with the values you want to set as input. So, according to the mesasge you are reporting, apparently anilwas passed to it — and it’s complaining that it can’t convert anilto aHash.Try something like this: