dimanche 4 janvier 2015

Can't create new entity in SQLite with DataMapper

I am new to Ruby world and Sinatra and I am trying to make a database using SQLite3 with Datamapper as ORM. I have installed the gems needed for this action. Here is how I make the db and the model:



DataMapper.setup(:default, "http://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 signup 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>


Here is how i handle the signup 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 log User.all it does not logs the users I have registered.


But if I run irb and type:


enter image description here


The new user stays in the database.


Any ideas why I can not create users runtime?


P.S. There are no errors logged in the console.


Aucun commentaire:

Enregistrer un commentaire