In last few hours, I was creating some application that could work in near future as a blog. But before that good to make some nice structure to have a base of application. So this post is about: how to create a blog using scaffolding. Scaffolding is a quick way to generate some of the most important pieces of an application. If I want to create models, view and also controllers for a new resource in a single operation scaffolding is a great way to make the job quick and easier.
Creating a new rails app is very easy. To do that, I can type only:
rails new scaffold_app
The whole structure of app is as:
To create an MVC components needed to post comments in the scaffold app directory we need to use scaffold generator:
rails generate scaffold post title:string body:text
And this generator creates post:
rails generate scaffold comment post_id:integer body:text
Creates post and comments tables in database:
Rails uses rake commands to run migrations:
Rake routes are the lists of all the URLs currently uses in the application.
After running rails server we can see that some very simple post and comments formulas were created. Good idea is to add some user with some unique id – integer type, public name – string type, email address – string type that will be used as a username. id, name, email attributes are columns in users table in a database.
To user should be associated each post that user will create:
rails generate scaffold Micropost content:string user_id:integer
So, for now, we should have scaffold application with:
- and adding each of them to database.
A few day ago I was struggling with some strange (for me as a beginner) problem with PostgreSQL database during generate some application skeleton. Running some: rails new app —database=postgresql and after that: rake db:migrate or rake db:create cause the fallowing problem:
I restart also database but the problem still appears. So I start to looking for some answer. First I check if my database it’s running, to perform some simple check:
ps auxww | grep postrges
This simple command should display all processes that are running and also connected with postres, but of corse, in my case, output was totally different. PostgreSQL was not running at all:
andrzejdubaj 19418 0,0 0,0 2442020 896 s003 S+ 12:43 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn postgres
I have PostgreSQL data files from a previous version of PostgreSQL and they are not compatible with current one. One solution was to delete all data and get a fresh database, basically to wipe data from PostrgreSQL completely including the user and data files.
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
from my rails application to get setup again. Next step was to check if my changes work as intended:
First process that is displayed gere is a masterserver process. The command shown for it are the same ones given when it was launched. Next two ones are the bacground workers processes launched automatically by the master process. Each of the remaining processes is a server process handling one client connection.
So I we can see this simple solution resolve problem with database on version 9.5.