Can't create new object in SQLite using DataMapper
I am new to Ruby and Sinatra world and I am trying to create a database using SQLite3 with Datamapper as ORM. I have installed the gems required for this action. This is how I am doing db and model:
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
class User
include DataMapper::Resource
property :id, Serial
property :email, String, :required => true
property :first_name, String
property :last_name, String
property :password_hash, String
property :password_salt, String
end
DataMapper.finalize
I have an HTML registration form:
<form action="/signup" method="POST" accept-charset="utf-8">
<input type="text" name="first_name" placeholder="First Name">
<input type="text" name="last_name" placeholder="Last Name">
<input type="mail" name="email" placeholder="Email">
<input type="password" name="password" placeholder="Password">
<button type="submit">Register me !</button>
</form>
This is how I handle the registration action:
post "/signup" do
password_salt = BCrypt::Engine.generate_salt
password_hash = BCrypt::Engine.hash_secret(params[:password], password_salt)
User.create(email: params[:email], first_name: params[:first_name], last_name: params[:last_name], password_hash: password_hash, password_salt: password_salt)
redirect to '/home'
end
When I execute this and after that I register User.all it does not register the users I have registered.
But if I run irb and typed:
The new user remains in the database.
Any ideas why I can't create the users runtime?
PS There are no errors in the console.
PS 2. I found that the password_salt and password_hash variables are the problem. Both are strings, when I pass them to the create function it doesn't work, but if I pass for example a "test" to the function it works fine. When I write these 2 variables down they seem fine too.
source to share
It seems that from property :password_hash, String
and property :password_salt, String
I am getting strings that are longer than the default length for the String method property
, which comes from the DataMapper.
Here's the solution:
property :password_hash, String, :length => 255
source to share