I want to build a very simple sinatra app, but I also want it to connect to the database. Here is the walk through for how to have a very base install, which includes migrations.
The final code is available at wschenk/sinatra-ar-template.
Setup the base
Add up your
A pretty standard
And now a
Rakefile, where we add runner tasks, docker tasks, as well
activerecord migration tasks.
Setup the app
First create a directory for the
Create a model
We'll begin by creating 2 directories, one that stores the model logic and the other which defines the routes
Let's add a model, for example
Then we can add our fields to it to it
Then create the table
And we can verify that it's there
First the model, where we tell it we need to have some required fields
Then the routes itself, where we either return a list of all the posts or we create a post and return it.
Testing it out
Start the server
Then we can test this out:
Add a post
Then we can see the results:
We can also try to add a post that's missing a required field:
Adding a password
Lets see how to add authentication.
Create the migration and run it:
Add the model and the route
Then lets add some routes for it:
Test account creation
Empty password confirmation
Not matched password confirmation
Trying a double signup
Login and store the cookie in the jar!
Pass in the session cookie
Create a model
Add a migration, for a table called for example
rake db:create_migration poi
Here is an example migration
Then we can run it:
Check out the table