By this time you get a welcome message when you hit http://localhost:3000/ in your browser.
Now let's make a real web application. We'll follow the steps in the welcome message, but in reverse order.
First, create our database.
Back to terminal, kill our server:
Ctrl-C
Get into MySQL:
mysql -uroot -p
Create a database (we are using utf-8 because we have worldwide user base):
create database betterblog_dev default character set = 'utf8' default collate = 'utf8_unicode_ci';
Create a database user:
create user 'bb_dev'@'localhost' identified by 'bb_pass';
And give him access to our newly created database:
grant all on betterblog_dev.* to 'bb_dev'@'localhost';
Let's test out this user. Go out:
exit
Then go back to MySQL using our new user:
mysql -ubb_dev -p betterblog_dev
If you can get in, great! Let's get out:
exit
Now we need to tell Rails about our new database and user.
cd /var/www/betterblog
gedit config/database.yml
Find a section labelled development and modify it as such (modification in
bold):
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: betterblog_dev
pool: 5
username: bb_dev
password: bb_pass
socket: /var/run/mysqld/mysqld.sock
Save and Exit.
That should take care of the database, now the next step: setup default rout and remove blah blah.. okay, let's go do stuff.
Let's get rid of this welcome message and put our own awesome welcome screen!
Back to terminal
cd /var/www/betterblog
rm public/index.html
That would take care of removing, and for our own welcome screen:
rails generate controller welcome index
Okay okay, let's see what do we have now!
rails server
Go to browser and hit http://localhost:3000/
What error?!?! It probably says: Please install the mysql2 adapter
Not bad a helpful message. Too bad it is misleading. What we really need to do it install an earlier versoin of one of our gem, specificially mysql2 gem. So we need to remove that one and install a different version.
Back to your terminal:
Ctrl-C
sudo gem remove mysql2
sudo gem install mysql2 -v 0.2.6
rm Gemfile.lock
rails server
Use your browser to hit http://localhost:3000/
Routing Error?
Ah yes.. routes. Let's fix that. Back to terminal:
Ctrl-C
gedit config/routes.rb
Find a line that says:
# root :to => "welcome#index"
And remove the first '#' so it looks like this:
root :to => "welcome#index"
Save and Exit
Back to terminal.
rails server
And browser..
and error.
openssl? Let's fix that too.
Back to terminal
Ctrl-C
sudo apt-get install libssl-dev
cd ~/sources/ruby-1.9.2-p180/ext/openssl
ruby extconf.rb
make
sudo make install
Let's try again
cd /var/www/betterblog
rails server
Finally, not an error. But it says find me at ...
let's go there.
Back to terminal.
Ctrl-C
gedit app/views/welcome/index.html.erb
Hey look, it is exactly what you see on the browser. Let's make it ours!
Delete everything and put in the following:
<h1>BetterBlog<h1>
<p>We are making a better blog.</p>
Save and Exit
Start the server again:
rails server
Reload your browser on http://localhost:3000/
Hurrah!