Setting up our Rails app
rails new quick_blog -T
Entering this command into your command prompt will cause Rails to generate a new application and begin to install dependencies for your application. This process may take a few minutes, so you should let it continue. Once it has finished type:
cd quick_blog
To change into the folder where your application is stored. If you look at the contents of this folder you’ll see:
This is the standard structure of a new Rails application. Once you learn this structure it makes working with Rails easier since everything is in a standard place. Next we’ll run this fresh application to check that our Rails install is working properly. Type:
rails server
Open your web-browser and head to: http://localhost:3000 you should see something that looks like:
Now that you’ve created the Rails application you should open this folder using Sublime. Open up Sublime, then open the quick_blog folder that was just generated.
Creating basic functionality
Now we’re ready to get started building an actual blog. In your command prompt press
Ctrl-c
to stop the Rails server, or open a new command prompt and navigate to your Rails application folder. Then you can use a Rails generator to build some code for you:rails g scaffold Post title body:text
You’ll be presented with something that looks like:
An important file that was generated was the migration file:
db/migrate/20130422001725_create_posts.rb
Note that you will have a different set of numbers in yours.class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.text :body t.timestamps end end end
This file is some Ruby code that is a database agnostic way to manage your schema. You can see that this code is to create a table called
Posts
and to create two columns in this table, a title column and a body column. Finally we need to instruct Rails to apply this to our database. Type:rake db:migrate
Once this command has run you can start up your Rails server again with
rails server
and then navigate tohttp://localhost:3000/posts to see the changes you’ve made to your application.
From here you can play around with your application. Go ahead and create a new blog post.
No comments:
Post a Comment